繁体   English   中英

Jquery select属性!=包含没有该属性的元素

[英]Jquery select attribute != includes elements that don't have the attribute

我试图选择与给定属性id不匹配的元素。 基于以下简化示例,为什么$(“td [groupId!= 1]”)返回甚至没有groupId属性的元素? 是否有另一种方法来选择具有groupId的元素,其中值不是1?

<table border="1">
<tr>
    <td>Group 1</td>
    <td groupId="1">1 - 1</td>
    <td groupId="1">1 - 2</td>
    <td groupId="1">1 - 3</td>
    <td>2</td>
    <td groupId="2">2 - 1</td>
    <td>3</td>
    <td groupId="3">3 - 1</td>
    <td>Total</td>
</tr>
<tr>
    <td>1</td>
    <td groupId="1">1</td>
    <td groupId="1">1</td>
    <td groupId="1">1</td>
    <td>2</td>
    <td groupId="2">2</td>
    <td>3</td>
    <td groupId="3">3</td>
    <td>0</td>
</tr>
<tr>
    <td>1</td>
    <td groupId="1">1</td>
    <td groupId="1">1</td>
    <td groupId="1">1</td>
    <td>2</td>
    <td groupId="2">2</td>
    <td>3</td>
    <td groupId="3">3</td>
    <td>0</td>
</tr>
</table>

尝试:

$("td[groupId][groupId!='1']")

这种类型的选择器将所有元素与属性groupId匹配,但只选择值不为1元素。

您可以执行以下操作,首先使用groupId!= 1选择所有td,然后选择具有从该jQuery集指定的groupId的那些:

$('td[groupId!= 1]').filter('[groupId]');

恕我直言,这是完全逻辑,td没有groupId有一个不会标记为1的groupId,因此被添加到结果集中。 或者把它放在代码中:

var x = {};
typeof x.foo === 'undefined'; //true
x.foo !== 1; //true

试试$("td[groupId][groupId != 1]")

这将选择所有具有groupId的td,然后选择该组中没有值1所有td。

PS。 这是not运算符的记录行为

暂无
暂无

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

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