Get element by Id from another page

后端 未结 3 1576
慢半拍i
慢半拍i 2021-01-16 12:02

I need to change content of page1 if page2 contain specified element. This code works great if I get id from the same page

if (document.getElementById(\"page         


        
3条回答
  •  我在风中等你
    2021-01-16 12:51

    Think this will work...

    var iframe = document.getElementById("iframeId"),
        doc = getFrameDocument(iframe);
    
    doc.getElementById("page2_element_id"); // [DOM Element]
    
    function getFrameDocument(iframe){
        return iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document;
    }
    

    return cond ? a : b means:

    if ( cond ) {
        return a;
    }
    else {
        return b;
    }
    

提交回复
热议问题