Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

javascript - jQuery / JavaScript:访问iframe的内容(jQuery/JavaScript: accessing contents of an iframe)

I would like to manipulate the HTML inside an iframe using jQuery.

(我想使用jQuery在iframe中操纵HTML。)

I thought I'd be able to do this by setting the context of the jQuery function to be the document of the iframe, something like:

(我认为我可以通过将jQuery函数的上下文设置为iframe的文档来做到这一点,例如:)

$(function(){ //document ready
    $('some selector', frames['nameOfMyIframe'].document).doStuff()
});

However this doesn't seem to work.

(但是,这似乎不起作用。)

A bit of inspection shows me that the variables in frames['nameOfMyIframe'] are undefined unless I wait a while for the iframe to load.

(一点检查显示出,除非等待iframe加载一会儿,否则frames['nameOfMyIframe']中的变量是undefined 。)

However, when the iframe loads the variables are not accessible (I get permission denied -type errors).

(但是,在iframe加载时,变量不可访问(我收到permission denied -类型错误)。)

Does anyone know of a work-around to this?

(有人知道解决方法吗?)

  ask by rz. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If the <iframe> is from the same domain, the elements are easily accessible as

(如果<iframe>来自相同的域,则可以很容易地访问元素)

$("#iFrame").contents().find("#someDiv").removeClass("hidden");

Reference

(参考)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...