简体   繁体   English

关于列表元素的单击更改类的jQuery

[英]jquery on click change class of list element

Here is my dynamically generated lists : 这是我动态生成的列表:

<ul class="config-swatch-list">

   <li class='color' onclick="color_select();">
        <a href="#" style="background-color: random_val"></a>
   </li>
   <li class='color' onclick="color_select();">
        <a href="#" style="background-color: random_val"></a>
   </li>
   <li class='color' onclick="color_select();">
        <a href="#" style="background-color: random_val"></a>
   </li>
   <li class='color' onclick="color_select();">
        <a href="#" style="background-color: random_val"></a>
   </li>
   <li class='color' onclick="color_select();">
        <a href="#" style="background-color: random_val"></a>
   </li>

</ul>

and the js function: 和js函数:

function color_select() {
    $('#color').click(function()
    {
        if ($(this).hasClass('active')) {
            console.log('clicked');
            $(this).removeClass('active');
        } else {
            $(this).addClass('active');
        }
    });
}

My problem is using this code when I clicked 1st li element, this js function add a active class successfully . 我的问题是当我单击1st li元素时使用此代码,此js函数成功添加了一个active类。 But this does not work for 2nd, or 4th li element (printing clicked in console).So the codes only working for 1st, 3rd , and 5th li elements. 但这不适用于2nd或4th li元素(在控制台中clicked打印)。因此代码仅适用于1st,3rd和5th li元素。 If I double click 2nd or 4th li element then the code working as expected. 如果我双击2nd或4th li元素,则代码将按预期工作。

All I want to do is after a click change a single li element css class to active or remove active class if there is already existing active class. 所有我想要做的就是后点击改变单一的li元素的CSS类active或删除active类,如果有已经存在active类。

Remove onclick attributes from <a> . <a>删除onclick属性。 And don't wrap click inside other function. 并且不要在其他函数内包装click And also use .color instead of #color ` 并且也使用.color代替#color

 $('.color').click(function() { if ($(this).hasClass('active')) { console.log('clicked'); $(this).removeClass('active'); } else { $(this).addClass('active'); } }); 
 .active{ color:orange; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="config-swatch-list"> <li class='color'> <a href="#" style="background-color: random_val">one</a> </li> <li class='color'> <a href="#" style="background-color: random_val">two</a> </li> <li class='color'> <a href="#" style="background-color: random_val">three</a> </li> <li class='color' > <a href="#" style="background-color: random_val">four</a> </li> <li class='color'> <a href="#" style="background-color: random_val">five</a> </li> </ul> 

jQuery.click() vs onClick .. you can still use onclick .. and use toggleClass() for active class instead of add/removeClass jQuery.click()vs onClick ..您仍然可以使用onclick ..并使用toggleClass()作为active类,而不是add/removeClass

 function color_select(el) { $('.color').removeClass('active').filter(el).toggleClass('active'); // remove active class from the li's and toggle the class for the clicked element } 
 .active{ color : red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="config-swatch-list"> <li class='color' onclick="color_select(this);"> <a href="#" style="background-color: random_val">Link 1</a> </li> <li class='color' onclick="color_select(this);"> <a href="#" style="background-color: random_val">Link 2</a> </li> <li class='color' onclick="color_select(this);"> <a href="#" style="background-color: random_val">Link 3</a> </li> <li class='color' onclick="color_select(this);"> <a href="#" style="background-color: random_val">Link 4</a> </li> <li class='color' onclick="color_select(this);"> <a href="#" style="background-color: random_val">Link 5</a> </li> </ul> 

But to be honest personally I don't prefer to use onclick= using a .click which presented by @Maheer answer is for me much much better 但说实话我个人不喜欢使用onclick=使用.click其中提出由@Maheer答案对我来说是非常非常好

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

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