Jquery hasClass用于多个类 [英] Jquery hasClass for multiple classes

查看:306
本文介绍了Jquery hasClass用于多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,

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

现在在Jquery我调用的函数就像,

Now in Jquery I am calling a function like,

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

如果我打电话给 $(this).hasClass(fixed),那么我需要只得到头而不是复选框,这在Jquery 1.4.2中工作得很好,但现在我更新到jquery 1.6.1。
现在我在条件中收到复选框。

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.

请帮助,

提前致谢

推荐答案

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

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

如果你想检查修复它是元素上的 only 类,那么没有jQuery就更容易完成,直接转到反映的属性:

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

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

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

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

当然,如果该元素上还有其他类,如固定的东西 - 其他。

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

如果你特别想要检查它已固定而没有chkCol,那么你必须故意这样做:

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

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

如果您在选择器中执行此操作(而不是检查已有的元素),则可以使用选择器 固定的:不是(.chkCol)。但是,虽然你可以使用 ,这是不必要的昂贵(jQuery必须解析选择器)。

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 can do that with an element you already have using is, it's unnecessarily expensive (jQuery has to parse the selector).

更新:你已经说过你已经尝试了&& !$ this.hasClass(chkCol)事情并没有奏效。这意味着中的某些早期如果条件已经确定了整个表达式的结果,那么<$ c的结果$ c>!$ this.hasClass(chkCol) part没有区别(并且根本没有运行,因为JavaScript短路表达式)。

Update: You've said that you've tried the && !$this.hasClass("chkCol") thing and it hasn't worked. That means that something earlier in the if condition 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).

示例:

var x = 1;

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

在这种情况下,因为 x 1 || 右边的任何内容都没有看到;表达式的值已经 true

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

更新2

你说你正在使用 hasClass ,但是在评论另一个回答你说:

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


如果我使用 $(this).attr('class')==修复,然后根本没有进入条件

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

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

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

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

注意className,而不是class 。如果那就是你正在做的事情,1.6.1上的将不起作用,但它将在1.4.2上。没有名为className的属性;有一个名为class的属性和一个名为className的属性。 jQuery 1.6.x区分属性和属性。

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

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

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

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

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

这篇关于Jquery hasClass用于多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆