我已经得到这个更早的工作,但由于某种原因,它不再有效。有人能解释我为什么现在不行吗?jquery通过iframe访问顶级文档
第一页包含的iframe:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Test 1</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="thediv" style="background: #ffff00;"> div </div>
<iframe id="theframe" src="test2.html"></iframe>
</body>
</html>
iframe内容的
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>test 2</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$("#clickme").click(function(){
$("#thediv", top.document).css("background","#ff0000");
alert($("#thediv", top.document).text());
});
});
</script>
</head>
<body>
<a href="#" id="clickme">clickme</a>
</body>
</html>
确定更多的测试它似乎iframe是行事像顶级文件..不知道为什么呢。 – Scott
好吧,如果我添加一个div到iframe内容的ID为“thediv”,代码会对该div做出反应而不是top.document ... – Scott