qwe*_*rty 4 jquery class parent toggle
我有一些列表元素,如下所示:
<li class="item">
<a class="toggle"><h4>ämne<small>2010-04-17 kl 12:54 by <u>nike1</u></small></h4></a>
<div class="meddel">
<span>
<img style="max-width: 70%; min-height: 70%;" src="profile-images/nike1.jpg" alt="" />
<a href="account.php?usr=47">nike1</a>
</span>
<p>text</p>
<span style="clear: both; display: block;"></span>
</div>
</li>
<li class="item odd">
<a class="toggle"><h4>test<small>2010-04-17 kl 15:01 by <u>nike1</u></small></h4></a>
<div class="meddel">
<span>
<img style="max-width: 70%; min-height: 70%;" src="profile-images/nike1.jpg" alt="" />
<a href="account.php?usr=47">nike1</a>
</span>
<p>test meddelande :) [a]http://youtube.com[/a]</p>
<span style="clear: both; display: block;"></span>
</div>
</li>
Run Code Online (Sandbox Code Playgroud)
我正在试图弄清楚如何使它成为当你点击类.toggle的链接时,父元素(li元素)将切换类.current.
我不确定以下代码是否正确,但它不起作用.
$(this).parent('.item').toggleClass('.current', addOrRemove);
Run Code Online (Sandbox Code Playgroud)
即使我删除"('.item')",它似乎没有任何区别.
那么,有什么想法吗?
提前致谢
toggleClass采用类名,而不是选择器.因此,你不应该包括..
将其更改为toggleClass('current')(不带a .).
此外,您可以通过调用closest代替而使代码更加健壮parent.在closest将搜索选择相匹配的最里面的父元素,使之保持如果链接嵌套在工作.item.