Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在iFrame中使用jQuery不起作用_Javascript_Jquery_Html_Iframe - Fatal编程技术网

Javascript 在iFrame中使用jQuery不起作用

Javascript 在iFrame中使用jQuery不起作用,javascript,jquery,html,iframe,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/jqu

我在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>
<head>
<title>test</title>
</head>
<body>
<div id="B">testcontent</div>
</body>
</html>

无标题文件
$(文档).ready(函数(){
$(“#A”).contents().find('#B').addClass('Z');
});
test.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>
<head>
<title>test</title>
</head>
<body>
<div id="B">testcontent</div>
</body>
</html>

测试
测试内容

通常在加载页面时,在源代码中,“Z”应该作为类添加,但它不是。有人知道问题出在哪里吗?两个文件都在同一个(本地)文件夹中。

您必须使用HTTP方案从Web服务器上运行它(
HTTP://
https://

在某些浏览器中,使用文件方案(
文件://
)可以防止这种跨帧访问。

试试这个

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

可能是在加载iframe的内容之前执行函数。尝试执行:$('iframe').ready(函数(){});取而代之,看看它是否有效。iframe上的ready没有任何用处。另一方面,加载可能会有所帮助。