在iFrame中使用jQuery无法正常工作

use*_*457 2 html javascript iframe jquery

我在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>
Run Code Online (Sandbox Code Playgroud)

的test.html:

<html>
<head>
<title>test</title>
</head>
<body>
<div id="B">testcontent</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

通常在加载页面时,在源中,"Z"应该作为类添加,但它不会.有谁知道问题可能是什么?两个文件都在同一(本地)文件夹中.

小智 7

试试这个

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

  • 更正:一旦在网络服务器上,这确实有效! (2认同)