在Javascript中,我试图自动化一个“onclick()”,但问题是我不知道如何将我的“onclick()”指向该感兴趣的元素。如果该行没有元素ID,如何自动执行“onclick”?
当元素确实有一个ID,虽然,我这样做:
var redbutton = "document.getElementById("red_button")"
if (redbutton) {
redbutton.onclick();
}
但是这一次,看着网页的HTML,我的“的onclick”的兴趣后,不会有一个ID,所以我不知道如何告诉我的浏览器点击它。 然而,前右其行,有一个ID:
<div id="buttoncolor_pane" style="background-color: rgb(50, 50, 50); position: absolute; width: 400px; height: 200px; top: 30px; left: 0px; z-index: 998; padding-top: 25px; background-position: initial initial; background-repeat: initial initial;">
<div style="width:140px; margin:10px auto; cursor:pointer" onclick="buttoncolor_submit('blue')">
<div style="width:140px; margin:10px auto; cursor:pointer" onclick="buttoncolor_submit('yellow')">
有没有一种方法,我可以直接把我的代码到该行,然后告诉它来进行“点击”?
如果这是你的代码,你应该添加一个类或ID,这将是正确的做法。 – user2580076