jQuery绑定和取消绑定委托 [英] jQuery binding and unbinding delegate

查看:101
本文介绍了jQuery绑定和取消绑定委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在jquery中绑定和取消绑定.delegate. 假设我使用此js即可轻松点击.

How can i bind and unbind a .delegate in jquery. Say i use this js for simple clicking.

function spanclick(){
    $(this).html('<input class="mm" value="' + $(this).text() + '"/>');
    $(this).unbind("click");
}
$("span.la").click(spanclick);
$("span.la").unbind("click");

但是现在我想切换到委托来代替简单的单击.
因为我当然想将其用于在页面加载时尚未创建的选择器.还是我需要使用.live()
问题: 我如何在jquery中bindunbind delegate's?

But now i want to switch to delegate in place of simple click.
Because as of course i want to use it to selectors that have not been created on the time of page load. or do i need to use .live()
Question: How do i bind and unbind delegate's in jquery ?

推荐答案

要使用委托,请使用span.la父元素(首选)或documentbody:

To use delegate, use span.la parent element (preferred) or document or body:

$(document).delegate('span.la', 'click', spanclick);

取消删除:

$(document).undelegate('span.la', 'click');

这篇关于jQuery绑定和取消绑定委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆