2014-03-19 121 views
0

我需要使用JavaScript/jQuery在iframe(带有id和名称)中单击按钮(不带id和名称)。如何在iframe中点击没有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> 
+0

我已更新帖子。 – user3270842

+0

[拦截点击iframe中的内容]的可能重复(http://stackoverflow.com/questions/2085752/intercept-click-on-content-in-an-iframe) –

回答

3

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

最接近你可能会到send a message文件,这将不得不包括JS听取消息和相应的回应。

1

你总是可以通过使用标签的ID或名字,你知道

例如

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

1

这是查找按钮的方式穿越它:

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

这将只在iframe源自服务器时起作用。这会找到第一个按钮,或<input>。如果有多个<input>,则可以为循环制作,并检查getAttribute('button');的“按钮”类型。 This将帮助您进行点击模拟。

相关问题