Home  >  Article  >  Web Front-end  >  How to determine whether to click the button in jquery

How to determine whether to click the button in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-16 15:38:144659browse

Jquery method to determine whether to click a button: first create a file and use the button tag to create three buttons; then obtain the currently clicked button object through [$(this)]; finally use the [attr()] method Get the id of the clicked button to determine which button was clicked.

How to determine whether to click the button in jquery

Recommended: "jquery video tutorial"

How to determine whether to click a button using jquery:

1. Create a new html file, named test.html, to explain how jquery determines which button is clicked.

How to determine whether to click the button in jquery

2. In the test.html file, introduce the jquery.min.js library file and successfully load the file before you can use the methods in jquery.

How to determine whether to click the button in jquery

3. In the test.html file, in the p tag, use the button tag to create three buttons. The button names are "Button 1" and "Button 2". , "Button 3", and set the class attribute of the three button buttons to btn.

How to determine whether to click the button in jquery

4. In the test.html file, set the id attributes of the button tags to btn1, btn2, and btn3. It is mainly used to obtain the id through jquery below to achieve judgment. That button was clicked.

How to determine whether to click the button in jquery

##5. In the test.html file, write the <script></script> tag. The js logic code of the page will be written in this tag.

How to determine whether to click the button in jquery

#6. In the js tag, when the page is opened through the ready() method, use the $ symbol to obtain the button object through the class name btn, and then use the bind() method to Each button button is bound to a click event. When the button is clicked, the currently clicked button object is obtained through $(this), the attr() method is used to obtain the id of the clicked button, and the id is output using the alert() method, thus Implementation of determining which button has been clicked.

How to determine whether to click the button in jquery

7. Open the test.html file in the browser and click the button to view the judgment results.

How to determine whether to click the button in jquery

Related free learning recommendations: JavaScript(Video)

The above is the detailed content of How to determine whether to click the button in jquery. 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