Added method: 1. Use the "$(".class name")" statement to obtain the element node that is set to a specified class; 2. Use "element node.click(function(){... })" statement adds a click event to the obtained element node.
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
In jquery, $()
is used to select elements. When using a class, you can use $(".class name")
to get and set a certain class. Elements.
Then you can use the click() method to add a click event to the obtained element.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function(){ $(".box").click(function(){ $(".box").css("background-color","#FFC0CB"); }); }); </script> </head> <body> <p class="box">2324 </p> <button class="box">点击,修改按钮的样式</button> </body> </html>
Description: The
click() method triggers the click event, or specifies the function to be run when the click event occurs.
Syntax: $(selector).click(function)
function: Optional. Specifies a function to run when a click event occurs.
Recommended related video tutorials: jQuery Tutorial (Video)
The above is the detailed content of How to add click event to a certain class element in jquery. For more information, please follow other related articles on the PHP Chinese website!