![](/img/trans.png)
[英]How can I use javascript to “click” a button that has no id or name?
[英]Using Javascript - How to Click a Submit Button that has No ID, Value or Name
如何使用javascript“點擊”沒有ID,值,類或名稱的按鈕?
該按鈕的相關代碼是:
<div class="classname anotherclassname" title="">
<div>
<button type="submit"><i class="anotherclass anotherclassname"></i>ImAButton</button>
</div>
</div>
我舉一個例子來說明我目前的情況,因為我知道這只是stackoverflow上的禮儀,但我甚至不知道從哪里開始。 我目前知道如何使用javascript單擊按鈕的唯一方法是使用:
document.getElementById("myButtonId").click();
這不適用於此。
如果您只支持現代瀏覽器和IE8及更高版本,那么您可以使用document.querySelectorAll
來選擇元素。 鑒於按鈕是頁面上提交類型的唯一按鈕,您可以執行以下操作:
document.querySelectorAll("button[type='submit']")[0].click();
querySelectorAll采用任何有效的CSS選擇器(IE8僅支持CSS2選擇器)。 因此,如果您需要使選擇更具體,您可以像使用任何CSS選擇器一樣使選擇器更具體。 像這樣的東西例如:
document.querySelectorAll(".classname button[type='submit'"])[0].click();
您可以使用頁面上的所有按鈕
buttons = document.getElementsByTagName('button');
document.getElementsByTagName('button')[0].click();
可能會點擊第一個按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.