Get all the button tag types

南楼画角 提交于 2020-05-26 11:44:25

问题


Is there a way to get all the button tag and their types on a particular page using javascript?


回答1:


var buttons = document.getElementsByTagName('button');
for (var i = 0; i < buttons.length; i++) {
    var button = buttons[i];
    var type = button.getAttribute('type') || 'submit'; // Submit is the default
    // ...
}



回答2:


I tried with the original answer with no success, so i use this :

var elements = document.querySelectorAll("input[type=button]");  

Example:

var elements = document.querySelectorAll("input[type=button]");   

for(var i = 0, len = elements.length; i < len; i++) {   
  console.log("Button: " +  elements[i].id);
}
 
    <input type="button" id="alfa" value="alfa">
    <input type="button" id="beta" value="beta">
    <input type="button" id="gamma" value="gamma">
    <input type="button" id="omega" value="omega">


来源:https://stackoverflow.com/questions/6564171/get-all-the-button-tag-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!