javascript - 在 iFrame 中使用 jQuery 不起作用

标签 javascript jquery html iframe

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

这是我的测试设置:

index.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”应该被添加为一个类,但事实并非如此。有谁知道问题可能是什么?这两个文件都在同一个(本地)文件夹中。

最佳答案

试试这个

$("iframe").load(function(e){
    $(this).contents().find('#B').addClass('Z');
});

关于javascript - 在 iFrame 中使用 jQuery 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18536947/

相关文章:

javascript - Bootstrap 选项卡 - 同时指向两个选项卡

jquery - 如何有条件地控制<li>的点击事件?

javascript - 未启用 Javascript 时的替代 anchor 标记

javascript - React JS fetch 作为 POST 跨域,PHP 返回 200 但没有正文响应

javascript - 有没有一种巧妙的方法可以将图片用作列表元素符号,然后在上面画一些东西?

javascript - Sprite 不显示在 html5 Canvas 上

javascript - 多次调用类构造函数

javascript - 将静态标签添加到传单中的圆形标记

javascript - jQuery $.ajax 调用——奇怪的返回函数问题

javascript - 从您的网站打开 Teamviewer