How to add JavaScript code into existing iFrame using jQuery?

前端 未结 8 2137
借酒劲吻你
借酒劲吻你 2020-12-13 08:13

I want to add a JavaScript snippet into an existing iFrame in the page using jQuery. I have the following code...

Code:

content = \"

        
8条回答
  •  甜味超标
    2020-12-13 08:37

    function putScriptInIframes(script, scriptId) {
    
       var $iframes = $('iframe');
       $iframes.each(function () {
            var thisDoc = this.contentWindow.document;
            if ( ! thisDoc.getElementById(scriptID)) {
                var scriptObj = thisDoc.createElement("script");
                scriptObj.type = "text/javascript";
                scriptObj.id = scriptId;
                scriptObj.innerHTML = script;
                thisDoc.body.appendChild(scriptObj);
            }
        });
    }
    

    This was the simplest way I could make it work.

提交回复
热议问题