javascript 单击页面上的所有按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24844566/
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
Click All Buttons on Page
提问by Dijji
I was wondering if anyone could help with a simple JavaScript code to click all buttons with the same value name/input name on a page?
我想知道是否有人可以帮助使用简单的 JavaScript 代码来单击页面上具有相同值名称/输入名称的所有按钮?
They each share:
他们各自分享:
<input name="all" type="submit" value="Do All">
Here's a screenshot of the buttons: http://oi61.tinypic.com/2nqblnr.jpg
这是按钮的屏幕截图:http: //oi61.tinypic.com/2nqblnr.jpg
Maybe one should make a button that works as a "click all" button? This will be a chrome extension (for personal use), so I can't really go about changing the "do all" button's code.
也许应该制作一个可以用作“单击全部”按钮的按钮?这将是一个 chrome 扩展(供个人使用),所以我不能真正去更改“全部完成”按钮的代码。
I was having a hard time finding it on Google, and I'm fine with HTML/CSS but really terrible with JavaScript.
我在谷歌上很难找到它,我对 HTML/CSS 很好,但对 JavaScript 真的很糟糕。
回答by AnonDCX
To click all the buttons on the page with the same name you can use getElementsByName() function and set it to a variable which will hold all the buttons in a NodeList then loop through the NodeList and call the click()
event for each button.
要单击页面上具有相同名称的所有按钮,您可以使用 getElementsByName() 函数并将其设置为一个变量,该变量将保存 NodeList 中的所有按钮,然后遍历 NodeList 并click()
为每个按钮调用事件。
For example:
例如:
// Get all buttons with the name 'all' and store in a NodeList called 'buttons'
var buttons = document.getElementsByName('all');
// Loop through NodeList and call the click() function on each button
for(var i = 0; i <= buttons.length; i++)
buttons[i].click();
回答by Himanshu Tyagi
I guess this is what you want to achieve :
我想这就是您想要实现的目标:
jsfiddle : http://jsfiddle.net/vqk2V/
jsfiddle:http: //jsfiddle.net/vqk2V/
Achieved using .click()
[http://api.jquery.com/click/]method of jquery.
使用jquery的.click()
[ http://api.jquery.com/click/]方法实现。
回答by antlis
// Select all buttons with class .btn
const inputBtns = document.querySelectorAll('[name="all"]');
// Select click all button
const clickAllBtn = document.querySelector('.click-all');
// Attach click handler to all input buttons
for(let i = 0; i < inputBtns.length; i++) {
inputBtns[i].addEventListener('click', function() {
alert(this.value);
})
}
// Click all buttons, when user click .click-all button
clickAllBtn.addEventListener('click', function() {
for(let i = 0; i < inputBtns.length; i++) {
inputBtns[i].click();
}
})
<input name="all" type="submit" value="Do All 1">
<input name="all" type="submit" value="Do All 2">
<button type="button" class="click-all">
click all
</button>
回答by AiApaec
With Java...maybe using Selenium. But you can use jquery (javascrpt's framework) so the code could be:
使用 Java...也许使用 Selenium。但是您可以使用 jquery(javascrpt 的框架),因此代码可以是:
<scrpipt>
function ClickAllButtons()
{
if($("input[type=button][name='someName']").length > 0)
{
$('input[type=button]').each(function(i){
var $currentButton = $(this);
$currentButton.click();
});
}
}
</script>
回答by Thimershen
What you would want to do is create a class eg.
您想要做的是创建一个类,例如。
public class MyClass{ ........ }
公共类 MyClass{ ........ }
..and then create methods which do the processing that is done when you click your buttons. Then call the methods with an object if your methods are non static in the specific buttons you require the method in.
..然后创建方法来执行单击按钮时完成的处理。然后,如果您的方法在需要该方法的特定按钮中是非静态的,则使用对象调用这些方法。
Now you can create a button which calls all the methods you want to call and it will do what you are asking for.
现在您可以创建一个按钮来调用您想要调用的所有方法,它会执行您的要求。