Javascript 获取所有按钮标签类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6564171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:18:36 来源:igfitidea点击:
Get all the button tag types
提问by user782400
Is there a way to get all the button tag and their types on a particular page using javascript?
有没有办法使用javascript获取特定页面上的所有按钮标签及其类型?
回答by Quentin
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
// ...
}
回答by Jorgesys
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">