Home > Web Front-end > JS Tutorial > body text

How does javascript trigger the clicked event of a button?

醉折花枝作酒筹
Release: 2023-01-07 11:45:05
Original
4602 people have browsed it

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();
}
Copy after login

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()
})
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template