Home > Web Front-end > JS Tutorial > body text

Detailed explanation of delegate and undelegate instances in jQuery

黄舟
Release: 2017-06-26 10:56:22
Original
1371 people have browsed it

Nothing to say, just look at the code!

##jQuery Version: 1.71

Browser version: chrome 21

<body>

    <ul class="a-list">
        <li><a href="javascript:;">test1</a></li>
        <li><a href="javascript:;">test2</a></li>
        <li><a href="javascript:;">test3</a></li>
        <li><a href="javascript:;">test4</a></li>
        <li><a href="javascript:;">test5</a></li>
    </ul>

    <ul class="button-list">
        <li><button>test1</button></li>
        <li><button>test2</button></li>
        <li><button>test3</button></li>
        <li><button>test4</button></li>
        <li><button>test5</button></li>
    </ul>

</body>
Copy after login
1 //绑定
2 $(document).delegate(&#39;a&#39;, &#39;click&#39;, function()
3 {
4     alert($(this).html());
5 });
Copy after login
1 //不能取消
2 $(document).undelegate(&#39;a&#39;);
3 
4 //可以取消
5 $(document).undelegate(&#39;a&#39;, &#39;click&#39;);
Copy after login
1 //绑定
2 $(document).delegate(&#39;a, button&#39;, &#39;click&#39;, function()
3 {
4     alert($(this).html());
5 });
Copy after login
//不能取消
$(document).undelegate(&#39;a&#39;, &#39;click&#39;);

//不能取消
$(document).undelegate(&#39;button&#39;, &#39;click&#39;);

//可以取消
$(document).undelegate(&#39;a, button&#39;, &#39;click&#39;);
Copy after login

Note:

1 //不能取消 (与绑定时选择器内容不一致,顺序颠倒)
2 $(document).undelegate(&#39;button, a&#39;, &#39;click&#39;);
3 
4 //不能取消 (与绑定时选择器内容不一致,少了一个空格)
5 $(document).undelegate(&#39;a,button&#39;, &#39;click&#39;);
Copy after login
1 //绑定
2 $(document).delegate(&#39;a, button&#39;, &#39;click mouseover&#39;, function(e)
3 {
4     alert($(this).html());
5 });
Copy after login
//略过部分测试,同上

//不能取消
$(document).undelegate(&#39;a&#39;, &#39;mouseover&#39;);

//可以取消
$(document).undelegate(&#39;a, button&#39;, &#39;click&#39;);

//可以取消
$(document).undelegate(&#39;a, button&#39;, &#39;mouseover&#39;);

//可以取消
$(document).undelegate(&#39;a, button&#39;, &#39;mouseover click&#39;);
Copy after login
1 //绑定
2 $(&#39;.a-list&#39;).delegate(&#39;a&#39;, &#39;click mouseover&#39;, function()
3 {
4     alert($(this).html());
5 });
Copy after login
//略过部分测试,同上
    
//不能取消
$(document).undelegate(&#39;a&#39;, &#39;click&#39;);

//可以取消
$(&#39;.a-list&#39;).undelegate(&#39;a&#39;, &#39;click&#39;);

//可以取消
$(&#39;ul&#39;).undelegate(&#39;a&#39;, &#39;click&#39;);

//可以取消
$(&#39;ul&#39;).undelegate();

//可以取消
$(&#39;.a-list&#39;).undelegate();
Copy after login

The above is the detailed content of Detailed explanation of delegate and undelegate instances 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