简体   繁体   English

jQuery 中的“没有类”选择器。 通过包含多个类的特定类名选择一个元素

[英]'Does not have class' selector in jQuery. Select an element by particular class name which holds multiple classes

I want to select an element by a particular class name (the element holds multiple classes).我想通过特定的类名选择一个元素(该元素包含多个类)。 I have referenced this answer but did not work for me.我已经参考了这个答案,但对我不起作用。

<i class="fas fa-star fa-lg clicked"></i>
<i class="fas fa-star fa-lg"></i>
<i class="fas fa-star fa-lg"></i>

clicked is a custom class with color:red inside. clicked是一个带有color:red内部的自定义类。

Javascript code : Javascript代码

$("i").click(()=> {
    changeColor();
})

const changeColor = () => {
    if (!$(this).hasClass('clicked')) {
        console.log('not clicked class')
    }
}

or或者

const changeColor = () => {
   if ($("i:not(.clicked)")){
     console.log("no clicked class")
   }
}

Both implementations do not work.两种实现都不起作用。 If you know why, please let me know.如果你知道为什么,请告诉我。

You are not capturing the this reference correctly, arrow notation can't use the implicit this reference, avoid using it with jQuery :您没有正确捕获this引用,箭头符号不能使用隐式this引用,避免将它与jQuery

An arrow function expression has a shorter syntax than a function expression and does not have its own this , arguments , super , or new.target .箭头函数表达式的语法比函数表达式短,并且没有自己的thisargumentssupernew.target These function expressions are best suited for non-method functions, and they cannot be used as constructors.这些函数表达式最适合非方法函数,它们不能用作构造函数。

I have reworked your code so it can work as you expect:我已经重新编写了您的代码,因此它可以按您的预期工作:

 $("i").click(function() { changeColor($(this)); }); const changeColor = (obj) => { if (!obj.hasClass('clicked')) console.log('not clicked class'); }
 .as-console {background-color:black !important; color:lime;} .clicked { color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <i class="fas fa-star fa-lg clicked">Button 1</i> <i class="fas fa-star fa-lg">Button 2</i> <i class="fas fa-star fa-lg">Button 3</i>

Instead of mentioned this you can use event.currentTarget instead:您可以使用event.currentTarget而不是提到this

 $("i").click(function(event) { changeColor($(event.currentTarget)); }); const changeColor = ($element) => { if (!$element.hasClass('clicked')) console.log('not clicked class'); }
 .clicked { color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <i class="fas fa-star fa-lg clicked">Button 1</i> <i class="fas fa-star fa-lg">Button 2</i> <i class="fas fa-star fa-lg">Button 3</i>

Using this in the code can be confusing because you need to have good knowledge how it works.在代码中使用this可能会令人困惑,因为您需要很好地了解它是如何工作的。 Many people tend to not use it because of that.因为这个,很多人倾向于不使用它。

Bear in mind that I renamed the obj to $element .请记住,我将obj重命名为$element I strongly recommend to use $ prefix to show that this variable holds a jQuery.我强烈建议使用$前缀来表明这个变量包含一个 jQuery。

Can you try using element.is('.clicked') ?您可以尝试使用element.is('.clicked')吗? You can try more than one combination with is你可以尝试用多台组合IS

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

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