问题
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