如何在iframe中找到div [英] How to find a div inside of an iframe
问题描述
我正在尝试使用jquery在iframe中查找div。有没有比我在下面使用的更好的方式?
I am trying to use jquery to find a div inside an iframe. Is there a better way than the one I'm using below?
$('#Iframe').contents().find('#MyDiv')
function atmslidein(){
$("#customer").ready(function(){
if($('#customer').attr('src')=='ATM.html')
{
$('#customer').contents().find('.atm_page').css('margin-left', '270px');
$('#customer').contents().find('.tele').css('display', 'none');
}
})
}
我已经尝试了将近一周时间来完成这项工作:
I've tried almost a week to make this work:
$('#Iframe').contents().find('#MyDiv')
这就是为什么我尝试了另一种方式来访问iframe的div。
That is why I tried another way to access the iframe's div.
无论如何,我发现了iframe文档应该有的东西,只有上面的函数正常工作:
Anyway I found something that the iframe document should have and only then the above function works properly:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
我的问题解决了,谢谢。 但是有人可以解释为什么这是必要的吗?
My problem is solved, thanks. But could someone explain why this is necessary?
推荐答案
我不认为 jQuery
可以这种方式访问contentWindow / Document的内容。在过去,我必须这样做:
I don't think the jQuery
can access the contentWindow/Document's content in that way. In the past, I've had to do this:
$('#iframe').each(function() {
$('#MyDiv', this.contentWindow.document||this.contentDocument);
});
**请注意 this.contentWindow || this.contentDocument
需要在IE和大多数其他浏览器上正常工作。获得内容窗口后,您需要选择文档。然后你可以使用jQuery来操作或遍历DOM,但前提是iframe的源位于同一个域中。
** Note that this.contentWindow||this.contentDocument
is required to work correctly across IE and most other browsers. Once you get the content window, you need to select the document. Then you can use jQuery to manipulate or traverse the DOM, but only if the iframe's source is in the same domain.
**更新以修复我们不在的地方的错误在我们获取contentWindow / Document之后指定文档。
** Updated to fix error where we don't specify the document after we get the contentWindow/Document.
这篇关于如何在iframe中找到div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!