我有这个标记:
<li class="parent">
<span class="button"></span>
<li class="parent">
<span class="button"></span>
<li class="parent">
<span class="button"></span>
</li>
<li>
<li>
现在我需要根据点击的按钮给每个家长上课。
所以我这样做:
var main_ctnr = $('.main_cntnr'),
main_prnt = main_ctnr.find('li.parent');
$('span.button').on('click', function(){
main_prnt.prevAll().addClass('some_class);
}
并解决需要:
点击第一个按钮后
<li class="parent some_class">
<span class="button"></span>
<li class="parent">
<span class="button"></span>
<li class="parent">
<span class="button"></span>
</li>
<li>
<li>
点击第二个按钮后
<li class="parent some_class">
<span class="button"></span>
<li class="parent some_class">
<span class="button"></span>
<li class="parent">
<span class="button"></span>
</li>
<li>
<li>
第三个
<li class="parent some_class">
<span class="button"></span>
<li class="parent some_class">
<span class="button"></span>
<li class="parent some_class">
<span class="button"></span>
</li>
<li>
<li>
它必须只在第一个父级中起作用,以便主父级的较早部分尚未收到该类。没有这种情况可能是,点击后this_button
:
<li class="parent some_class">
<span class="button"></span>
<li class="parent">
<span class="button"></span>
<li class="parent">
<span class="button"></span>
</li>
<li>
<li>
<li class="parent some_class">
<span class="this_button"></span>
<li class="parent">
<span class="button"></span>
<li class="parent">
<span class="button"></span>
</li>
<li>
<li>
这在山上很苛刻 - 非常感谢您的帮助。