Home > Article > Web Front-end > How to solve the problem that jquery on does not take effect
The solution to jquery on not taking effect: first open the corresponding code file; then use the correct method "$('body').on('click', '#test .evt', function() {alert($(this).text())});” Just use on.
The operating environment of this tutorial: Windows 7 system, jquery version 1.10.0. This method is suitable for all brands of computers.
Recommendation: "jquery tutorial"
jquery's on() binding method is invalid
The element in front of on It must also exist in the dom when the page is loaded. Dynamic elements or styles can be placed in the second parameter of on
The jQuery on() method is an officially recommended method for binding events. Use the on() method to bind specified events to dynamic elements created dynamically in the future, such as append, etc.
<div id="test"> <div class="evt">evt1</div> </div>
Wrong usage, the following method only binds the click event for the first class div of evt, while the div dynamically created using append is not bound.
<script> // 先绑定事件再添加div $('#test .evt').on('click', function() {alert($(this).text())}); $('#test').append('<div class="evt">evt2</div>'); </script>
The correct usage is as follows :
<script> $('body').on('click', '#test .evt', function() {alert($(this).text())}); $('#test').append('<div class="evt">evt2</div>'); </script>
checkbox radio selection setting If ef32da46e2c234ae705f71c3bfd6d43d is added dynamically, you can do this
$("#grid").on("click","input[name=ck]",function(){ $("input[name=ck]").not(this).prop("checked",false); });
Note: $(selector).on (event,childSelector,data,function,map)
The above is the detailed content of How to solve the problem that jquery on does not take effect. For more information, please follow other related articles on the PHP Chinese website!