如何在iframe中单击没有ID或名称的按钮?

时间:2014-03-19 14:14:27

标签: javascript jquery button iframe click

我需要使用JavaScript / jQuery在iframe(带有id和名称)中单击按钮(没有id和名称)。

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript">
</script>
</head>
<body>
<iframe name="abb" id="abb" src="http://example.com" scrolling="no"frameborder="0" style="border:none; overflow:hidden; width:45px; left:-19px; height:21px; z-index: 0; position: relative;" allowTransparency="true"></iframe>
</div>

<script>
document.getElementById("abb").contentWindow.document.getElementsByName('REG_BTN')[0].click();
</script>
</body>
</html>

3 个答案:

答案 0 :(得分:3)

您无法访问其他来源的文档DOM。

你最接近的可能是文件的send a message,该文件必须包含听取消息的JS并做出相应的回复。

答案 1 :(得分:1)

您总是可以使用您知道的标签的ID或名称来遍历它

例如

$('body div.container div.panel>div.heading~div button').click();

答案 2 :(得分:0)

这是一种找到按钮的方法:

var iframe = document.getElementById('iframeId'),
    innerDocument = iframe.contentDocument || iframe.contentWindow.document,
    button = innerDocument.getElementsByTageName('input')[0];

仅当iframe来自您的服务器时才会有效。这将找到第一个按钮,或<input>。如果有多个<input>您可以创建一个 for循环并检查“按钮”&#39;输入getAttribute('button');This将帮助您进行点击模拟。

相关问题