jquery事件

来源:互联网 发布:微信小程序erp软件 编辑:程序博客网 时间:2024/06/05 17:17

data:

获取:$(this).data('industry');

$('.skill').toggle(      function(){         //选中         $(this).css("background-color","lightblue");         $(this).css('color','white');         $(this).attr("data-state",'1');      },      function(){         $(this).css("background-color","white");         $(this).css('color','black');         $(this).attr("data-state",'0');      });



$("#btn_search").trigger("click");



失败:不能单一去p1

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){    $("p").live("click",function(){   $(this).slideToggle();    });    $("button").click(function(){    $("#p1").die();  });});</script></head><body><p id="p1">这是一个段落。</p><p class="c1">这是另一个段落。</p><p>请点击任意 p 元素,段落会消失。包括本段落。</p><button>移除通过 live() 方法向 p 元素添加的事件处理程序</button></body></html>

成功:

$(document).ready(function(){    $("#p1").live("click",function(){   $(this).slideToggle();    });    $("button").click(function(){    $("#p1").die();  });});


引:http://zhidao.baidu.com/link?url=b-ZafcLcFkVvK0vFN0MbdEqHcbQ7N6ijMlgFAYDm2AsMB3l4vmd31KUaqkvSGEi0DS7an-StTjOolbzlWxx2Oa

jQuery 1.9 已经将 live 和 die 移除,取而代之的是 on 和 off


1
2
3
$("#crawl_web ul li span").off('click');
$("#crawl_web ul li input").off('focus').off('blur');
$("#crawl_web ul li span").on('click',function(){

补充一下1楼的答案,live是给所有匹配的元素附加一个事件处理函数,即使这个元素是以后再添加进来的,但on、off方法是不能用来匹配以后添加进来的元素的。

比如点击按钮用ajax在某个div(id="xxx")上加载了一些元素(class="yyy"),那么操作这些元素(class="yyy")就不能直接用:

1
2
3
4
$(".yyy").on("focus",function(){
    // 操作
    .....
});

应该改成:

1
2
3
4
$("#xxx").on("focus",".yyy",function () {
    // 操作
    .....
});


0 0
原创粉丝点击