Javascript Jquery hasClass 多个类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6280119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:04:38  来源:igfitidea点击:

Jquery hasClass for multiple classes

javascriptjquery

提问by Max

I am having,

我有,

<tr valign="top">
    <th class="chkCol fixed">..checkbox..</th>
    <th class="fixed">..head..</th>
</tr>

Now in Jquery I am calling a function like,

现在在 Jquery 中,我正在调用一个函数,例如,

if($(this).hasClass("fixed")){
   ....
}

If I call $(this).hasClass("fixed"), then I need to get only head not checkbox and that is working perfect in Jquery 1.4.2 but now I updated to jquery 1.6.1. Now I am getting checkbox inside if condition.

如果我调用$(this).hasClass("fixed"),那么我只需要获取头部而不是复选框,这在 Jquery 1.4.2 中运行良好,但现在我更新到了 jquery 1.6.1。现在我在 if 条件下得到复选框。

Please Help,

请帮忙,

Thanks in advance

提前致谢

回答by T.J. Crowder

I'd be very surprised if jQuery 1.4.2 gets this wrongjQuery 1.4.2 does not get this wrong. hasClass("fixed")should be true in both cases, in all versions of jQuery. Here's an exampleusing v1.6.1, and the same exampleusing v1.4.2. Both work fine.

如果 jQuery 1.4.2 出错,我会感到非常惊讶jQuery 1.4.2 没有出错。hasClass("fixed")在这两种情况下,在所有版本的 jQuery 中都应该是 true。这是使用 v1.6.1的示例,以及使用 v1.4.2的相同示例。两者都工作正常。

If you want to check that "fixed" it's the onlyclass on an element, that's more easily done without jQuery, by going directly to the reflected property:

如果你想检查“fixed”它是元素上唯一的类,没有 jQuery 更容易完成,通过直接转到反射属性:

if (this.className === "fixed") {
    // ...
}

classNameis a property reflecting the "class" attribute, and like the "class" attribute it's a space-delimited string.

className是反映“class”属性的属性,与“class”属性一样,它是一个以空格分隔的字符串。

The above will fail, of course, if the element has any other class on it as well, like "fixed something-else".

当然,如果元素上也有任何其他类,例如“修复其他内容”,则上述操作将失败。

If you specifically want to check it has "fixed" and doesn't have "chkCol", then you have to do that on purpose:

如果您特别想检查它是否“已修复”并且没有“chkCol”,那么您必须故意这样做:

var $this = $(this);
if ($this.hasClass("fixed") && !$this.hasClass("chkCol")) {
    // ...
}

If you were doing this in a selector (rather than checking an element you already had) you could use the selector ".fixed:not(.chkCol)". But although you cando that with an element you already have using is, it's unnecessarily expensive (jQuery has to parse the selector).

如果您在选择器中执行此操作(而不是检查您已有的元素),则可以使用选择器".fixed:not(.chkCol)"。但是,尽管您可以使用已经使用的元素做到这一点is,但它不必要地昂贵(jQuery 必须解析选择器)。



Update: You've said that you've tried the && !$this.hasClass("chkCol")thing and it hasn't worked. That means that something earlierin the ifcondition has already determined the outcome of the expression as a whole, and so the result of the !$this.hasClass("chkCol")part makes no difference (and isn't getting run at all, as JavaScript short-circuits expressions).

更新:你说过你已经尝试过这个&& !$this.hasClass("chkCol")东西,但没有奏效。这意味着条件中较早的某些内容if已经确定了整个表达式的结果,因此该!$this.hasClass("chkCol")部分的结果没有任何区别(并且根本不会运行,因为 JavaScript 会短路表达式)。

Example:

例子:

var x = 1;

if (x == 1 || $this.hasClass("fixed") && !$this.hasClass("chkCol"))

In that case, because xis 1, nothing to the right of the ||is even looked at at all; the expression's value is already true.

在那种情况下,因为xis 1||甚至根本看不到 右边的任何东西;表达式的值已经是true



Update 2:

更新2

You've said you're using hasClass, but in a comment on another answer you said:

您已经说过您正在使用hasClass,但在对另一个答案的评论中您说:

If i use $(this).attr('class') == "fixed", then not at all going into if condition

如果我使用$(this).attr('class') == "fixed",则根本不会进入 if 条件

I don't suppose what you're actually doing is this:

我不认为你实际上在做什么是这样的:

if ($(this).attr('className') == "fixed") {
    // ...
}

Note that's "className", not "class". If that's what you're doing, that will not workon 1.6.1 but it will on 1.4.2. There is no attributecalled "className"; there's an attribute called "class" and a propertycalled "className". jQuery 1.6.x differentiates between attributes and properties.

请注意,这是“className”,而不是“class”。如果这就是你正在做的,那将不会在 1.6.1 上工作,但会在 1.4.2 上工作。没有名为“className”的属性;有一个名为“class”的属性和一个名为“className”的属性。jQuery 1.6.x 区分属性和属性。

My code above, using hasClass, will work on all versions. Or change your code to use attr("class")rather than attr("className").

我上面的代码使用hasClass,适用于所有版本。或者更改您的代码以使用attr("class")而不是attr("className").

Here's a v1.4.2 exampleshowing attr("className")working (which it shouldn't). Here's a v1.6.1 exampleshowing attr("className")not working (which is correct). Both versions show the correct way to get at the "class" attribute.

这是一个显示attr("className")工作的 v1.4.2 示例(它不应该)。这是一个显示attr("className")不工作的 v1.6.1 示例(这是正确的)。两个版本都显示了获取“class”属性的正确方法。

回答by Pointy

You could either do this:

你可以这样做:

if ($(this).attr('className') === 'fixed')

or else:

要不然:

if ($(this).not('.chkCol').hasClass('fixed'))

I think it'd be better to make sure that "chkCol" elements never get the "fixed" class in the first place.

我认为最好首先确保“chkCol”元素永远不会获得“固定”类。

edit— the ever-perceptive @T. J. Crowder points out correctly that ".attr()" is, as of jQuery 1.6, the wrong way to get property values from elements. There's a degree of angelic pin dancing involved in the explanation of why that's the case, but suffice to say that you'd now use:

编辑——敏锐的@TJ Crowder 正确地指出,“.attr()”是从 jQuery 1.6 开始,从元素获取属性值的错误方法。在解释为什么会这样的原因中有一定程度的天使针舞,但足以说明您现在会使用:

if ($(this).prop('className') === 'fixed')

回答by Xion

Seems like the update has fixed a bug, then :) hasClass("fixed")should definetely be true for both thelements here.

似乎更新修复了一个错误,那么 :)hasClass("fixed")对于th这里的两个元素都应该是正确的。

As for your problem, either explicitly exclude chkColclass:

至于你的问题,要么明确排除chkCol类:

if ($(this).hasClass("fixed") && !$(this).hasClass("chkCol")) {

or find some other criteria identifying the element you are looking for (tag name, ID, another dedicated class, etc.).

或者找到一些其他标准来标识您正在寻找的元素(标签名称、ID、另一个专用类等)。

回答by DanielB

Well, then it must have been a bug in jQuery 1.4.2 because obviously the <th class="chkCol fixed">..checkbox..</th>has the class "fixed".

好吧,那么它一定是 jQuery 1.4.2 中的一个错误,因为显然它<th class="chkCol fixed">..checkbox..</th>已经“修复”了这个类。

If you want to get a element with the only class "fixed" you could do

如果你想获得一个只有“固定”类的元素,你可以这样做

($(this).attr('class') == "fixed")

回答by Alexis No

Maybe you should use attr() instead:

也许你应该使用 attr() 代替:

if($(this).attr("class") == "fixed"){
   ....
}