我试图使用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');
}
})
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了将近一周的时间来完成这项工作:
$('#Iframe').contents().find('#MyDiv')
Run Code Online (Sandbox Code Playgroud)
这就是为什么我尝试了另一种方式来访问iframe的div.
无论如何,我发现iframe文档应该有的东西,只有上面的函数正常工作:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Run Code Online (Sandbox Code Playgroud)
我的问题解决了,谢谢.但有人可以解释为什么这是必要的吗?
Hac*_*ese 13
我认为jQuery不能以这种方式访问contentWindow/Document的内容.在过去,我必须这样做:
$('#iframe').each(function() {
$('#MyDiv', this.contentWindow.document||this.contentDocument);
});
Run Code Online (Sandbox Code Playgroud)
**请注意,this.contentWindow||this.contentDocument需要在IE和大多数其他浏览器上正常工作.获得内容窗口后,您需要选择文档.然后,您可以使用jQuery来操纵或遍历DOM,但前提是iframe的源位于同一个域中.
**更新以修复错误,我们在获取contentWindow/Document后未指定文档.