如何更改div元素中的url,该元素由iframe使用javascript加载
在my manifest.json中:如何更改div元素中的url,该元素由iframe使用javascript加载,javascript,jquery,iframe,google-chrome-extension,Javascript,Jquery,Iframe,Google Chrome Extension,在my manifest.json中: "content_scripts": [ { "matches": [ "http://*/*", "https://*/*" ], "css":["style.css"], "js": ["./js/jquery-3.3.1.js","content-script.js", "./search-api/search-ap
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"css":["style.css"],
"js": ["./js/jquery-3.3.1.js","content-script.js", "./search-api/search-api.js"],
"run_at": "document_end"
}
],
在content-script.js中
var height = "40px";
var iframe = document.createElement("iframe");
iframe.src = chrome.extension.getURL("toolbar.html");
iframe.style.height = height;
iframe.style.width = "100%";
iframe.style.position = "fixed";
iframe.style.top = "0";
iframe.style.left = "0";
iframe.style.zIndex = "938089"; // Some high value
// Etc. Add your own styles if you want to
document.documentElement.appendChild(iframe);
// continuing add-toolbar.js
var bodyStyle = document.body.style;
var cssTransform = 'transform' in bodyStyle ? 'transform' : 'webkitTransform';
bodyStyle[cssTransform] = 'translateY(' + height + ')';
toolbar.html:
<div style="color:aqua">
<a target="_blank" href="https://www.uber.com" id="toolbars">Uber cool home page</a>
</div>
你可以这样做
$(‘#iframID’).attr(‘src’,’your url’);
有一个完美的答案。最简单的方法是使用JavaScript或jquery
document.getElementById(“工具栏”).setAttribute(“href”,“www.amazon.com”)代码>
$(“#工具栏”).attr('href','www.amazoncom')代码>
要访问嵌入iframe中的元素,首先需要访问它,然后使用获取它的元素。在您的情况下,这看起来像:
$('iframe').contents ().find ('#toolbars').attr ('href', 'www.amazon.com');
使用jquery或不使用jquery?请尝试以下操作:
$('#toolbars',parent.document).attr('href','www.amazon.com')代码>jquery,当然:)@wayneOS parent.document做什么?我想知道它指的是什么。@kai对不起,我误解了你的意思。您需要的是$('iframe').contents().find('toolbars').attr('href','www.amazon.com')代码>。需要parent.document在iframe上下文之外更改父窗口中的内容。
$('iframe').contents ().find ('#toolbars').attr ('href', 'www.amazon.com');