Html Javascript:自动点击按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17144459/
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-29 10:10:43  来源:igfitidea点击:

Javascript: Automatically clicking a button?

javascripthtmlgoogle-chromegoogle-chrome-extensiononclick

提问by KingPolygon

I'm learning how to write a chrome extension, and I'm fairly new to javascript.

我正在学习如何编写 chrome 扩展程序,而且我对 javascript 还很陌生。

Here's some html:

这是一些html:

<div class="button data" style="">
   <a class="button1 whiteColor" href="http://link1.com">VIEW This</a>
   <a class="button2 redColor" href="http://link2.com">VIEW That</a>
</div>

What I want to do is open link2.com by automatically clicking button2 using javascript.

我想要做的是通过使用 javascript 自动单击 button2 来打开 link2.com。

I'm using the following, but it's not working :/

我正在使用以下内容,但它不起作用:/

document.getElementByClassName("button2 redColor").click();

Any help would be appreciated!!

任何帮助,将不胜感激!!

回答by jQuery00

document.getElementsByClassName("button2 redColor")[0].click();

You need select index, because getElementsByClassName return the array

您需要选择索引,因为 getElementsByClassName 返回数组