【发布时间】:2014-03-23 13:06:21
【问题描述】:
我正在尝试访问正在 IFrame 中显示的按钮。然后我想为那个按钮附加一个监听器。但是,我似乎无法访问 IFrame。假设按钮的 id 是button。我怎么能这样做。
这是我的示例代码:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<script>
function test() {
var temp = $("myIframe").contents();
alert(temp);
var temp2 = temp.find("body");
alert(temp2);
var temp3 = temp2.html();
alert(temp3);
}
</script>
<h:form>
<p:commandButton value="Click" onclick="test();"/>
</h:form>
<iframe id="myIframe" src="http://test.com" height="200" width="500"/>
</h:body>
</html>
temp 和 temp2 的警报有效,但 temp3 未定义。有人可以帮忙吗?
【问题讨论】:
-
IFRAME 中的页面是否位于不同的站点上?
-
是的。它可以是任何网站。我希望能够根据需要向按钮添加侦听器。
-
您无法访问其他域的 IFRAME
-
有没有办法在没有 IFrame 的情况下完成我想要的?也许使用
-
您可以使用 jQuery 检索 html,然后将其插入到您的 dom 中。 (Embed 和 iframe 一样有跨域限制)
标签: javascript jquery html jsf iframe