Javascript 如何以编程方式单击按钮,然后执行某些操作?

Javascript 如何以编程方式单击按钮,然后执行某些操作?,javascript,jquery,Javascript,Jquery,我试图编写一个脚本,通过编程单击一个选项卡,在网站上显示更多的div,然后对这些显示的div执行一些操作 现在,我有 document.getElementByIdbuttonid.click;//显示网页的另一部分 console.log$idofnewdiv; …但是它似乎没有看到新的div。但是,当我手动单击按钮时,console.log能够正确地打印出来。我做错什么了吗?你也可以这样使用它: var button = document.getElementById("buttonid"

我试图编写一个脚本,通过编程单击一个选项卡,在网站上显示更多的div,然后对这些显示的div执行一些操作

现在,我有

document.getElementByIdbuttonid.click;//显示网页的另一部分 console.log$idofnewdiv;


…但是它似乎没有看到新的div。但是,当我手动单击按钮时,console.log能够正确地打印出来。我做错什么了吗?

你也可以这样使用它:

var button = document.getElementById("buttonid");

button.addEventListener("click", function(){
   alert("Button is Clicked");
});

button.click();
var mButton = document.getElementById("buttonId");

mButton.onClick(function(){

   your code to execute here.

});
看起来你的点击功能什么都没有做。下面的代码假定所有的都已隐藏

var listOfDivsToReveal = document.getElementsByClassName("divsToReveal");
var listOfDivsIter = 0;
$("#thebuttonID").click(function(){
    if(listOfDivsIter > listOfDivsToReveal.length){
        //do whatever you do when all of your <div> tags are revealed
    }
    listOfDivsToReveal[listOfDivsIter++].style.display = "inline";//Or your desired display.
});

单击时没有任何操作,如果您正在使用jQuery,请尝试最大限度地利用它们为您提供的功能:多读书