Home > Web Front-end > HTML Tutorial > The difference between on() and click() in jQuery

The difference between on() and click() in jQuery

巴扎黑
Release: 2017-03-30 14:04:33
Original
1890 people have browsed it

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>
Copy after login



jQuery Click event

<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(".add").click(function(){
    $(".li").append(&#39;<li>I am new item.<button class="delete">Delete</button></li>&#39;);
});
$(".delete").click(function(){
    $(this).parent().remove();
});
</script>
Copy after login

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(&#39;click&#39;,&#39;.delete&#39;,function(){
    $(this).parent().remove();
});
Copy after login

         

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!

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