开发者

How to access a DIV inside a iframe from a parent

开发者 https://www.devze.com 2023-01-03 03:54 出处:网络
I am trying to access a div id=\"mnuGrp\" th开发者_高级运维at resides inside a child iframe(id=\"iframe2) from the parent window within the document.ready() section but unsure how to access this child

I am trying to access a div id="mnuGrp" th开发者_高级运维at resides inside a child iframe(id="iframe2) from the parent window within the document.ready() section but unsure how to access this child div in the parent inorder to apply fadein and fadeout calls.

I want to be used within the following call:

$(' ???? ').click(function(){ etc

where "????" is my means to access the child div within the iframe.


Since you're asking in a jQuery context specifically:

$(document).ready( function(){
    $('#iframe2').contents().find('div#mnuGrp').fadeIn('slow');
    // or whichever effect you prefer
});

Note that you will likely be constrained by the Same Origin Policy with respect to the main page and the frame source.


You can access the div in this way: window.frames[framename].document.getElementById("mnuGrp")


Everyone loves this contents().find() not sure why.
jQuery isn't required. But if you wanted to use jQuery:

$("#mnuGrp", $("#iframe2")[0].contentWindow.document).click();
0

精彩评论

暂无评论...
验证码 换一张
取 消