jquery批量绑定click事件

时间:2021-11-26 04:54:49

jquery批量绑定click事件:

var selects = $(".public_select dd ul li");
debugger;
/*$(".public_select dd ul li a").each(function(){
/!*$(this).click(function(){
alert("aa");
})*!/
alert("cc");
$(this).bind("click", function(){
alert("Hello World bind");
}); })*/
selects.bind("click", function(){
var a = $(this);
var b = $(this).children("a")[0];
var op = $(this).children("a")[0].attributes[0].value;
debugger;
$(".public_select dd ul li a").removeAttr("style");
$(".public_select dd ul li a[op="+op+"]").css("background","#a7c2fe");
});

因为a标签绑定不上click(原因未知),改为li绑定事件,li标签与a标签一一对应。

<dl class="public_select" id="status">
<dt op="0">未提交</dt>
<dd>
<ul>
<li><a op="">--请选择--</a></li>
<li><a op="0">未提交</a></li>
<li><a op="1">已提交</a></li>
</ul>
</dd>
</dl>

注释掉的参考代码:

//var selects = $(".public_select dd ul li a");
/*$(".public_select dd ul li a").each(function () {
$(this).click(function () {
alert("aa");
/!*this.
var op = this.attr("op");
$(".public_select dd ul li a").css("background","none");
$(".public_select dd ul li a[op="+op+"]").css("background","#a7c2fe");*!/
})
/!*var op = this.attr("op");
$(".public_select dd ul li a").css("background","none");
$(".public_select dd ul li a[op="+op+"]").css("background","#a7c2fe");*!/
})*/