簡體   English   中英

如何在iframe中單擊沒有ID或名稱的按鈕?

[英]How to click a button with no id or name, inside an iframe?

我需要使用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>

您無法訪問其他來源的文檔的DOM。

最接近的可能是將消息發送到文檔,該文檔必須包含偵聽該消息並做出相應響應的JS。

您總是可以使用您確實知道的標記的ID或名稱來遍歷它

例如

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

這是一種定位按鈕的方法:

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

僅當iframe來自您的服務器時,此方法才有效。 這將找到第一個按鈕或<input> 如果有多個<input> ,則可以進行for循環,並使用getAttribute('button');檢查'button'類型getAttribute('button'); 將幫助您進行點擊模擬。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM