Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改div元素中的url,该元素由iframe使用javascript加载_Javascript_Jquery_Iframe_Google Chrome Extension - Fatal编程技术网

如何更改div元素中的url,该元素由iframe使用javascript加载

如何更改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

在my manifest.json中:

"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

  • JavaScript:

    document.getElementById(“工具栏”).setAttribute(“href”,“www.amazon.com”)

  • JQuery:

    $(“#工具栏”).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');