[英]remove class and add class on single element
I have table type structure made of DIV
elements. 我有由
DIV
元素组成的表类型结构。
I have made a expand/collapse type list. 我制作了一个展开/折叠类型列表。
I have this icon that i want to change if it expands and reset it back if I collapse the list. 我有这个图标,如果它展开则我想要更改,如果我折叠列表则重置它。 The two icons are linked to 2 classes.
这两个图标链接到2个类。
$('.rowHead_but i', this ).removeClass('icon-double-angle-down');
and 和
$('.rowHead_but i', this ).addClass('icon-double-angle-up');
Okay it removes the class successfully and adds the new class, but it does it for all rows. 好吧,它成功删除了类并添加了新类,但是它为所有行执行了。
I only want this
row to be affected. 我只希望
this
行受到影响。
What Im I doing wrong? 我做错了什么?
<div class="notificationDetail"> <div class="header"> <div class="Collapse"> <div class="rowHead1"><span>2014-10-10</span></div> <div class="rowHead_but"><i class="icon-double-angle-down"></i></div> </div> </div> <div class="header"> <div class="Collapse"> <div class="rowHead2"><span>2014-10-10</span></div> <div class="rowHead_but"><i class="icon-double-angle-down"></i></div> </div> </div> <div class="header"> <div class="Collapse"> <div class="rowHead3"><span>2014-10-10</span></div> <div class="rowHead_but"><i class="icon-double-angle-down"></i></div> </div> </div> </div>
JQuery code JQuery代码
$('.notificationDetail').on('click','.header', function(e){
e.stopPropagation();
$(function() {
var classTst_down = $('.rowHead_but i', this ).hasClass( "icon-double-angle-down" );
var classTst_up = $('.rowHead_but i', this ).hasClass( "icon-double-angle-up" );
if ( classTst_down ) {
console.log('remove down');
$(this ).removeClass('icon-double-angle-down');
console.log('removed');
$(this ).addClass('icon-double-angle-up');
console.log('added');
}
if ( classTst_up ){
console.log('remove up');
$(this ).addClass('icon-double-angle-down');
$(this ).removeClass('icon-double-angle-up');
}
});
});
From what you understand is that you don't actually want to bind event handler to .header
container, but rather to individual .Collapse
items. 根据您的理解,您实际上并不想将事件处理程序绑定到
.header
容器,而是绑定到单个.Collapse
项目。 if so you can then dramatically simplify your code if you use toggleClass
: 如果是这样,如果使用
toggleClass
则可以大大简化代码:
$('.notificationDetail').on('click', '.Collapse', function (e) {
e.stopPropagation();
$('.rowHead_but i', this).toggleClass('icon-double-angle-down icon-double-angle-up');
});
Seems overly complicated. 似乎过于复杂。 Why do you need two classes?
你为什么需要两节课? - try something like:
- 尝试类似的事情:
$('.notificationDetail .header').click(function() {
var $el = $(this).find('.rowHead_but i');
$el.toggleClass('icon-double-angle-down');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.