jQuery綁定事件

來源:互聯網
上載者:User

標籤:cli   dom   input   off   charset   func   ext   pen   inpu   

1.事件綁定的方式

事件DOM:三種綁定方式jQuery:#前面幾種調用的全是on$(‘.c1‘).click()$(‘.c1‘).bind(‘click‘,function(){})$(‘.c1‘).unbind(‘click‘,function(){})$(‘.c1‘).delegate(‘a‘,‘click‘,function(){}) #不同於其它,有委託的作用$(‘.c1‘).undelegate(‘a‘,‘click‘,function(){})$(‘.c1‘).on(‘click‘,function(){})$(‘.c1‘).off(‘click‘,function(){})

 

 

2. 由於程式是從上往下執行,所以對新輸入的沒有綁定alert事件。

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body>    <input id="t1" type="text"/>    <input id="a1" type="button" value="添加"/>    <ul id="ul">        <li>123</li>        <li>456</li>    </ul>    <script src="jquery-1.12.4.js"></script>    <script>        $(‘#a1‘).click(function(){            var v=$(‘#t1‘).val();            var temp="<li>"+v+"</li>";            $(‘#ul‘).append(temp);        });        $(‘ul li‘).click(function(){            v=$(this).text();            alert(v);        })    </script></body></html>

  所以需要重新綁定

3. delegate具有委託的作用,不同於其它幾個。

click不行,bind不行,on不行,delegate可以。委託。

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body>    <input id="t1" type="text"/>    <input id="a1" type="button" value="添加"/>    <ul id="ul">        <li>123</li>        <li>456</li>    </ul>    <script src="jquery-1.12.4.js"></script>    <script>        $(‘#a1‘).click(function(){            var v=$(‘#t1‘).val();            var temp="<li>"+v+"</li>";            $(‘#ul‘).append(temp);        });        $(‘ul‘).delegate(‘li‘,‘click‘,function(){            v=$(this).text();            alert(v);        })    </script></body></html>

 

jQuery綁定事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.