【发布时间】:2017-05-23 12:16:31
【问题描述】:
我正在尝试使用 iframe 对象 和 jQuery 访问特定元素(可能更类似于此),但它不起作用。
iframeWindow 对象是not null,但下一条语句似乎不起作用。我在这个答案上看到了类似this 的东西,但它不起作用。我做错了吗?
这是我的代码:
RADIO.PHP
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function(){
setTimeout(function(){
var iframe= document.getElementById("iframe");
var iframeWindow = iframe.contentWindow;
var text=iframeWindow.$("div:nth-child(3) .c2").html();
console.log(text);
//DOESN'T PRINT "INNER MOST"
}, 1000);
});
</script>
</head>
<body>
<div class="c1">
<iframe id="iframe" src="api.php" height="200" width="300">
</iframe>
</div>
</body>
</html>
API.PHP
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script type="text/javascript" src="jquery.js"></script>
<body id="abody">
Hey
<div class="c1"></div>
<div class="c1">
<p class="c2"></p>
</div>
<div class="c1">
<p class="c2">
INNER MOST
</p>
</div>
</body>
</html>
编辑:我已经纠正了语法错误。
【问题讨论】:
-
请注意,您的 HTML 无效。您不能将
p元素相互嵌套,因此 DOM 结构与您的选择器不匹配 -
我已经编辑了请检查一下
-
iframeWindow !== inframeWindow也检查一下这个。
标签: javascript php jquery html iframe