append element in head of an iframe using jquery

后端 未结 4 1743
刺人心
刺人心 2021-01-17 23:21

i want to append a style sheet(css) link to the head of an iframe using jquery . i tried with the following code but not working.

$(\'#tabsFrame\').contents(         


        
4条回答
  •  执念已碎
    2021-01-18 00:19

    This could be related to IE not allowing you to add elements in the DOM, check out the clever solution here

    EDIT:

    Thanks @kris, good advice to add more info in case links break:

    Here is the main code snippet from the link, in case it goes out again. (This is only needed with some IE version, for the most part, the other answer work just fine)

    var ifrm;
    
    //attempts to retrieve the IFrame document    
    function addElementToFrame(newStyle) {
        if (typeof ifrm == "undefined") {
            ifrm = document.getElementById('previewFrame');
            if (ifrm.contentWindow) {
                ifrm = ifrm.contentWindow;
            } else {
                if (ifrm.contentDocument.document) {
                    ifrm = ifrm.contentDocument.document;
                } else {
                    ifrm = ifrm.contentDocument;
                }
            }
        }
    
        //Now that we have the document, look for an existing style tag
        var tag = ifrm.document.getElementById("tempTag");
    
        //if you need to replace the existing tag, we first need to remove it
        if (typeof tag != "undefined" || tag != null) {
            $("#tempTag", ifrm.document).remove();
        }
    
        //add a new style tag
        $("HEAD", ifrm.document).append("");
    }
    

提交回复
热议问题