Home >Web Front-end >JS Tutorial >How does javascript trigger the clicked event of a button?

How does javascript trigger the clicked event of a button?

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-22 14:53:274664browse

btnObj.click() is to actually use a program to click the button, triggering the onclick() event of the button. It can be called when the button is not hidden, but not after the button is hidden. Do not use it if it is hidden. Visiable attribute, use "style.display=none".

How does javascript trigger the clicked event of a button?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

js triggers button click event

    function load(){    //下面两种方法效果是一样的
    document.getElementById("target").onclick();
    document.getElementById("target").click();
}

Note:

1.btnObj.click() is to actually use a program to click the button, triggering the button's onclick() event , it can be called when the button is not hidden, but not after the button is hidden. Do not use the Visiable attribute to set the hiding, use style.display=none

2.btnObj.onclick() just simply calls btnObj The method pointed to by onclick just calls the method and does not trigger the event.

jQuery triggers the button click event

$(function(){
                
  // test 的点击事件
  $("#test").click(function(){
                
    alert("点击了");
  });
                
  // 调用 test 的点击事件的两种方法
   $("#test").trigger("click");   $("#test").click()
})

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How does javascript trigger the clicked event of a button?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn