Accessing the document object of a frame with JavaScript

后端 未结 2 2021
青春惊慌失措
青春惊慌失措 2020-12-01 13:14

I\'m testing on this page, and I\'m not sure what I\'m missing.

// Two frames on the page
> document.getElementsByTagName(\"frame\").length
2

// Same dom         


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 13:36

    The all-around way to getting a frame's contents is with something like this:

    var theFrame = document.getElementsByTagName("frame")[0];
    var theFrameDocument = theFrame.contentDocument || theFrame.contentWindow.document;
    var button = theFrameDocument.getElementById("mybutton");
    

    However, it is possible to get a 's document by using its name, like:

    window.frames["frame_name"].document
    

    if the HTML were:

    ...
    

提交回复
热议问题