【问题标题】:Insert a Script into a iFrame's Header, without clearing out the body of the iFrame将脚本插入 iFrame 的标头,而不清除 iFrame 的正文
【发布时间】:2011-02-09 01:28:24
【问题描述】:

我希望将脚本添加到 iFrame 的标头,同时不会丢失 iFrame 的正文或标头中包含的所有内容...

这就是我现在所拥有的,它确实使用新脚本更新了 iFrame,但它清除了 iframe 中的所有内容,而不是我想要的附加内容。谢谢!乙

    // Find the iFrame
    var iframe = document.getElementById('hi-world');

    // create a string to use as a new document object
    var val = '<scr' + 'ipt type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></scr' + 'ipt>';

    // get a handle on the <iframe>d document (in a cross-browser way)
    var doc = iframe.contentWindow || iframe.contentDocument;
    if (doc.document) { doc = doc.document;}

    // open, write content to, and close the document
    doc.open();
    doc.write(val);
    doc.close();

【问题讨论】:

    标签: javascript jquery dom iframe window


    【解决方案1】:
    var headID = document.getElementsByTagName("head")[0];         
    
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = 'yourpath';
    headID.appendChild(newScript);
    

    【讨论】:

      猜你喜欢
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      相关资源
      最近更新 更多