1

美好的一天,我想通过 BIND 方法将 click 方法附加到 div。我的附加代码是

$("#brend_list > #mCSB_1 > .mCSB_container > div > a").bind('click',function(){
id=$(this).attr('id');
ttl=$(this).parent().children("span").html();
state=$(this).hasClass("checked");

if(state) {$(this).removeClass("checked");
$("#"+id+"_filter").remove();
}
else{$(this).addClass("checked");


$("#filters").append(" <div style='display:inline-block;' id='"+id+"_filter' class='filter_brands' onclick='brend_uncheck(\""+id+"\"); $(this).remove();'><span>"+ttl+"</span><b>x</b></div> ");


}});

我的 div 结构是

<div id="brend_list" class="mCustomScrollbar _mCS_1">
  <div class="mCustomScrollBox mCS-light" id="mCSB_1" style="position:relative; height:100%; overflow:hidden; max-width:100%;">
   <div class="mCSB_container" style="position: relative; top: 0px;">
    <div><a id="filt_brend001"></a><span>Loreal Paris</span></div><!-- this <a> -->
    <div><a id="filt_brend002"></a><span>Laura Mercier</span></div>
    ...........
    </div>
    <div class="mCSB_scrollTools" style="position: absolute; display: block;">
      <div class="mCSB_draggerContainer"><div class="mCSB_dragger" style="position: absolute; height: 110px; top: 0px;" oncontextmenu="return false;">
        <div class="mCSB_dragger_bar" style="position: relative; line-height: 110px;">  
        </div>
      </div>
      <div class="mCSB_draggerRail">
      </div>
     </div>
    </div>
   </div>
</div>

但它不附加。

4

1 回答 1

2

你应该要么改变

$("#brend_list > #mCSB_1 > .mCSB_container > div > a")

$("#brend_list > #mCSB_1 > .mCSB_container > div > a + span")

上面的一个小提琴是here

或者,将 HTML 从

<div><a id="filt_brend001"></a><span>Loreal Paris</span></div>

<div><a id="filt_brend001"><span>Loreal Paris</span></a></div>

在这里摆弄上面的动作。

于 2013-03-31T06:51:13.510 回答