[英]Add and remove class on a p element
I am trying to add a class called active
to the p
children of a div
using a click event. 我正在尝试使用click事件向div
的p
子项添加一个名为active
的类。 The first click adds the class to the p
, but if I click on the second div the previous inserted class must be removed. 第一次单击将类添加到p
,但如果我单击第二个div,则必须删除先前插入的类。
Here is a very basic example: 这是一个非常基本的例子:
$('.changeP').on('click',function(){ $(this).removeClass('active'); $(this).children('p').addClass('active'); });
.active { color: orange; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-3 col-sm-3 col-xs-6"> <div class="changeP"> <img src="http://via.placeholder.com/350x150"> <p>Lorem ipsum loret</p> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <div class="changeP"> <img src="http://via.placeholder.com/350x150"> <p>Lorem ipsum loret</p> </div>
You ned to remove the class from all elements that have it, not just the clicked element. 您需要从包含它的所有元素中删除该类,而不仅仅是单击的元素。 To do that change $(this)
to $('.active')
. 要做到这一点,请将$(this)
更改$(this)
$('.active')
。 Also note that your outer div
elements are missing a closing tag. 另请注意,您的外部div
元素缺少结束标记。 Try this: 尝试这个:
$('.changeP').on('click', function() { $('.active').removeClass('active'); $(this).children('p').addClass('active'); });
.active { color: orange; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-3 col-sm-3 col-xs-6"> <div class="changeP"> <img src="http://via.placeholder.com/350x150"> <p>Lorem ipsum loret</p> </div> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <div class="changeP"> <img src="http://via.placeholder.com/350x150"> <p>Lorem ipsum loret</p> </div> </div>
here is your solution: 这是你的解决方案:
$('.changeP').on('click',function(){
$('p').removeClass('active');
$(this).find('p').addClass('active');
});
First, remove class active
from all .changeP p
elements (paragraphs inside changeP
), then add the class to the clicked one: 首先,从所有.changeP p
元素( changeP
段落)中删除class active
,然后将该类添加到单击的一个:
$('.changeP').on('click', function() { $('.changeP p').removeClass('active'); // remove active class from all '.changeP p' $(this).children('p').addClass('active'); // add it to this p });
.active { color: orange; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-3 col-sm-3 col-xs-6"> <div class="changeP"> <img src="http://via.placeholder.com/350x150"> <p>Lorem ipsum loret</p> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <div class="changeP"> <img src="http://via.placeholder.com/350x150"> <p>Lorem ipsum loret</p> </div> </div> </div>
I guess you are looking for jQuery toggleClass
function which adds the class if not added previously and removes if already added. 我猜您正在寻找jQuery toggleClass
函数,如果之前没有添加该类,则添加该类,如果已添加则删除。
$('.changeP').on('click', function() {
$(this).children('p').toggleClass('active');
});
Try this code: 试试这段代码:
$('.changeP').click(function(){
var $this = $(this),
$p = $this.find('p'),
$activeP = $('.changeP .active');
if(!$p.hasClass('active')) {
$activeP.removeClass('active');
$p.addClass('active');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.