简体   繁体   English

.click函数,点击后删除类

[英].click function, remove class after clicked

I have this function so if I click for example ITEM 1 , the sample1 and sample3 will become red because have that specific class ( class1 ). 我有这个功能,所以如果我点击例如ITEM 1sample1sample3将变为红色,因为有特定的类( class1 )。

The problem is that if I click on another item (for example ITEM2 ), the red items of ITEM1 will remain red and I need them to turn black and highlight in red ONLY the items of current clicked class in the first list. 问题是,如果我点击另一个项目(例如ITEM2 ), ITEM1的红色项目将保持红色,我需要它们变为黑色,并在红色突出显示第一个列表中当前点击的类的项目。

What to add to below ready(function() in order to do that? Thank you in advance! 添加到下面准备好了什么(function()才能做到这一点?提前谢谢!

<ul>
    <li class="leftcol class1">ITEM 1</li>
    <li class="leftcol class2">ITEM 2</li>
    <li class="leftcol class3">ITEM 3</li>
    <li class="leftcol class4">ITEM 4</li>
</ul>

<ul>
    <li class="rightcol class1 class4">sample1</li>
    <li class="rightcol class2 class3">sample2</li>
    <li class="rightcol class3 class1">sample3</li>
    <li class="rightcol class4 class2">sample4</li>
</ul>

This is the function: 这是功能:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>
    $(document).ready(function() {
        $('.leftcol').click(function() {
            $('.rightcol[class*=' + this.className.split(" ").pop() + ']').css('color', 'red');    
        });
    });
</script>

Use classes to hold the styles, and then just remove the class on all elements, and add it back on the elements matching the class etc 使用类来保存样式,然后只删除所有元素上的类,并将其添加回与类匹配的元素等

 $(document).ready(function() { $('.leftcol').click(function() { $('.rightcol').removeClass('red') .filter('.'+ this.className.split(" ").pop()) .addClass('red'); }); }); 
 .red {color: red} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="leftcol class1">ITEM 1</li> <li class="leftcol class2">ITEM 2</li> <li class="leftcol class3">ITEM 3</li> <li class="leftcol class4">ITEM 4</li> </ul> <ul> <li class="rightcol class1 class4">sample1</li> <li class="rightcol class2 class3">sample2</li> <li class="rightcol class3 class1">sample3</li> <li class="rightcol class4 class2">sample4</li> </ul> 

Since you are not using class to style items, logic is this, on each click reset color of all items with class .rightcol and after reset add red to one u want. 由于您没有使用类来设置项目样式,因此逻辑就是这样,在每次单击重置颜色时,使用类.rightcol重置所有项目,并在重置后将红色添加到您想要的项目。 Try something like this: 尝试这样的事情:

 $(document).ready(function() {
 $('.leftcol').click(function() {
$('.rightcol').css('color', 'black');
$('.rightcol[class*=' + this.className.split(" ").pop() + ']').css('color',     'red');
});
});

Have you tried using a variable in which you store the name of the class that you last changed its color from? 您是否尝试过使用一个变量来存储上次更改其颜色的类的名称? So whenever your function is called you can change the color back to default and update the variable. 因此,无论何时调用函数,您都可以将颜色更改回默认值并更新变量。 Another option would be to first set all classes colors to default and then execute the line to change the color to red. 另一种选择是首先将所有类颜色设置为默认值,然后执行该行以将颜色更改为红色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM