我正在尝试使用 jquery 在 iframe 中查找 div。有没有比我下面使用的方法更好的方法?
$('#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');
}
})
}
我已经尝试了近一周来完成这项工作:
$('#Iframe').contents().find('#MyDiv')
这就是为什么我尝试了另一种方法来访问 iframe 的 div。
不管怎样,我找到了 iframe 文档应该有的东西,只有这样上面的功能才能正常工作:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
我的问题解决了,谢谢。但有人可以解释为什么这是必要的吗?
我不认为jQuery
可以通过这种方式访问 contentWindow/Document 的内容。过去,我不得不这样做:
$('#iframe').each(function() {
$('#MyDiv', this.contentWindow.document||this.contentDocument);
});
** 注意this.contentWindow||this.contentDocument
需要在 IE 和大多数其他浏览器中正常工作。进入内容窗口后,您需要选择文档。然后,您可以使用 jQuery 来操作或遍历 DOM,但前提是 iframe 的源位于同一域中。
** 更新以修复我们在获取 contentWindow/Document 后未指定文档的错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)