HTML page code
<p>
<h1>Click</h1>
<button class="add">Click me to add new item</button>
<ul class="li">
<li>I am old item.<button class="delete">Delete</button></li>
<li>I am old item.<button class="delete">Delete</button></li>
<li>I am old item.<button class="delete">Delete</button></li>
</ul>
</p>
jQuery Click event
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(".add").click(function(){
$(".li").append('<li>I am new item.<button class="delete">Delete</button></li>');
});
$(".delete").click(function(){
$(this).parent().remove();
});
</script>
The above example You can find that the li added through the button cannot be deleted because it is a newly added HTML code and has no click event bound. The solution: replace the click event with an on event. The code is as follows:
$(".li").on('click','.delete',function(){
$(this).parent().remove();
});
The above is the detailed content of The difference between on() and click() 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