[英]jQuery addClass to <li> when <a> is clicked
I'm trying to add a class to the li when the a.more is clicked. 我试图在单击a.more时向li添加一个类。 But it isn't working.
但这不起作用。
<ul>
<li id="post-4" class="post">
<div class="text">
<span class="date">12/12/2013</span>
<h3>The Title</h3>
<div class="post-excerpt">
<p>The excerpt will show</p>
<a class="more" href="#">read more..</a>
</div>
<div class="post-content">
<p>The full content</p>
<a class="less" href="#">close</a>
</div>
</div>
</li>
</ul>
jQuery: jQuery的:
jQuery(function () {
jQuery('.post').on('click', 'a.more', function(){
jQuery(this).addClass('active');
});
jQuery('.post').on('click', 'a.less', function() {
jQuery(this).removeClass('active');
});
});
add parents(). 添加父母()。 to addclass on li
在李上添加类
jQuery('.post').on('click', 'a.more', function(){
jQuery(this).parents('li').addClass('active');
});
jQuery('.post').on('click', 'a.less', function() {
jQuery(this).parents('li').removeClass('active');
});
Try above code. 尝试上面的代码。
Since the user can only see one of the two links at any moment, you could use a single function 由于用户随时只能看到两个链接之一,因此您可以使用一个功能
jQuery('.post').on('click', 'a.more, a.less', function() {
jQuery(this).closest('li').toggleClass('active');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.