在 iFrame 中使用 jQuery 不起作用

2024-01-20

我在 iFrame 中使用 jQuery 时遇到问题。

这是我的测试设置:

索引.html:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
    $("#A").contents().find('#B').addClass('Z');    
});

</script>
</head>
<body>

<iframe id="A" src="test.html" style="width:700px; height: 1000px;" frameborder="0"></iframe>

</body>
</html>

测试.html:

<html>
<head>
<title>test</title>
</head>
<body>
<div id="B">testcontent</div>
</body>
</html>

通常,当加载页面时,在源代码中,“Z”应该作为类添加,但事实并非如此。有谁知道问题可能是什么?两个文件位于同一(本地)文件夹中。


try this

$("iframe").load(function(e){
    $(this).contents().find('#B').addClass('Z');
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 iFrame 中使用 jQuery 不起作用 的相关文章

随机推荐