[英]remove class and add class when an element is clicked
jQuery is already working, what i want to achieve is that when title is clicked i want to remove the class of the i tag which is fa-toggle-off and replace it with fa-toggle-on. jQuery已经在工作了,我想要实现的是当点击标题时我想删除i标签的类,它是fa-to-off-off并用fa-to-switch-on替换它。 I already achieve this the problem is all of the i tag is affected. 我已经实现了这个问题,所有的i标签都受到了影响。
<div class="row package-row">
<div class="global-switch">
<input type="checkbox" id="switch1" name="switch1" class="switch" />
<label for="switch1">Hide Packages</label>
</div><!--end of global-switch-->
<div class="col-md-12">
<div class="header-details">
<h3 id="toggle-h3-package"><i class="fa fa-toggle-off" style="margin-right: 10px;"></i>TITLE</h3>
<p>DESCRIPTION</p>
</div>
<div class="global-control" id="global-control">
CONTENT
</div>
</div>
</div>
jQuery jQuery的
$('.toggle-h3-package').click(function(){
$(this).parent().next('.global-control').animate({
height:'toggle',
opacity:'toggle'
},{
duration: 5000,
specialEasing: {
width: "linear",
height: "easeOutBounce"
}
});
});
$('.toggle-h3-package').click(function() {
var target = $('.header-details .toggle-h3-package i');
if (target.hasClass('fa-toggle-off')) {
$('.header-details .toggle-h3-package i').removeClass('fa-toggle-off').addClass('fa-toggle-on');
}
else {
$('.header-details .toggle-h3-package i').removeClass('fa-toggle-on').addClass('fa-toggle-off');
}
});
You should narrow your selector: 你应该缩小你的选择器:
var target = $('.header-details .toggle-h3-package i');
- this will find all i
elements on the page. - 这将在页面上找到所有i
元素。 And you need only i
that is in you clicked .toggle-h3-package
. 而你只需要i
就是在你点击.toggle-h3-package
。 Use find
method with this
: 使用find
方法与this
:
$('.toggle-h3-package').click(function() {
var target = $( this ).find( 'i' );
// your code here
Also you can use toggleClass
function without checking if element has some class: 您也可以使用toggleClass
函数而不检查元素是否具有某个类:
target.toggleClass('fa-toggle-on fa-toggle-off');
This is what i did as you suggested.. 这就是我建议你做的..
$('.toggle-h3-package').click(function() {
var target = $( this ).find( 'i' );
target.toggleClass('fa-toggle-on fa-toggle-off');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.