简体   繁体   English

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

[英]Using jQuery inside iFrame not working

I have a problem using jQuery inside an iFrame. 我在iFrame中使用jQuery时遇到问题。

Here is my test setup: 这是我的测试设置:

index.html: 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>

test.html: 的test.html:

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

Normally when the page is loaded, in the source, "Z" should be added as a class, but it doesn't. 通常在加载页面时,在源代码中,“Z”应该作为一个类添加,但它不会。 Does anyone have an idea what the problem might be? 有谁知道问题可能是什么? Both files are in the same (local) folder. 两个文件都在同一(本地)文件夹中。

try this 尝试这个

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

You have to run it from a webserver with the HTTP scheme ( http:// or https:// ). 您必须使用HTTP方案( http://https:// )从Web服务器运行它。

Using the file scheme ( file:// ) prevents this sort of cross frame access in some browsers. 使用文件方案( file:// )可以防止某些浏览器中出现这种跨帧访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM