我有一个表,其中包含逗号分隔的数字列表,如下所示:72,76,81
。我正在尝试选择不包含特定数字的表格单元格。此选择器有效:
$("td:not(:contains('76'))", $table)
问题是可能有包含“576”或“761”等的行 为了解决这个问题,
我决定在每个数字周围放置一个
,这样现在就变成:72,<跨度>76、<跨度>81
。我的选择器现在是:
$("td:not(:contains('76'))", $table)
当我在 Firebug 中调试此选择器时,它是返回一些 HTML 源代码中实际不存在的 span 标记,而不是正确的
。
基本上,将 '
' 放入 :contains()
字符串中会破坏选择器。是否有任何类似于 :contains()
的选择器在将 HTML 传递给它时可以正常工作?或者有什么方法可以通过
内的整个字符串进行选择?
顺便说一句,这个网站使用的是 jquery 1.3.2。
I have a table that contains comma separated lists of numbers like this: <td>72,76,81</td>
. I'm trying to select the table cells that don't contain a particular number. This selector worked:
$("td:not(:contains('76'))", $table)
The issue is that there may be rows that contain '576' or '761', etc.
To solve that issue, I decided to put a <span>
around each number, so that it's now: <td><span>72</span>,<span>76</span>,<span>81</span></td>
. My selector is now:
$("td:not(:contains('<span>76</span>'))", $table)
When I debug this selector in Firebug, it's is returning some span tag that doesn't actually exist in the HTML source, instead of the correct <td>
s.
Basically, putting the '<span>
' in the :contains()
string is breaking the selector. Is there any selector similar to :contains()
that will work properly when HTML is passed to it? Or is there some way I can select by the entire string inside the <span>
?
BTW, this site is using jquery 1.3.2.
发布评论
评论(6)
如果您想坚持使用
,即使使用 jQuery 1.3.2,这也可以:
http://jsfiddle.net/mattball/PFk8H/
另一方面,如果您想回到旧的标记,实际上非常容易:
http://jsfiddle.net/mattball/L6AFz/
要在正则表达式中使用变量,请使用
new RegExp(string)
构造函数语法:http://jsfiddle.net/mattball/hKQH7/
If you want to stick with
<span>
s, this works, even with jQuery 1.3.2:http://jsfiddle.net/mattball/PFk8H/
On the other hand, it's actually really easy if you want to go back to the old markup:
http://jsfiddle.net/mattball/L6AFz/
To use a variable in the regex, use the
new RegExp(string)
constructor syntax:http://jsfiddle.net/mattball/hKQH7/
试试这个:
$("td span:not(:contains('76'))", $table).parent()
try this:
$("td span:not(:contains('76'))", $table).parent()
您可能无法为此使用
:contains
。正如您所说:contains('76')
将匹配 '76'、'576'、'761' 等。您可以尝试使用.filter
来获取你想要什么。演示:http://jsfiddle.net/KvK3J/
You may not be able to use
:contains
for this. As you've stated:contains('76')
will match '76', '576', '761', etc. You can try using.filter
to get what you want.Demo: http://jsfiddle.net/KvK3J/
使用 jQuery 1.2.6 进行测试
http://jsfiddle.net/G27JF/4/
Tested with jQuery 1.2.6
http://jsfiddle.net/G27JF/4/
为了避免“包含”问题,您可以将它们作为类:
并且:
这也可以调整为在 1 个对象中包含多个:
或者甚至放入 TD 本身,从而进一步简化选择器
To avoid the 'Contains' issue, you would make them as classes:
And:
This could be adjusted to have multiples in 1 object too as:
Or even put in the TD itself simplifying the selector even more
我将使用正则表达式来匹配表行的内部。首先迭代所有元素,然后忽略与正则表达式匹配的元素
这是一个将匹配的基本元素:
查看 RegExr 构建更多正则表达式。
I would use a regular expression to match the insides of the table row. First iterate over all the elements then ignore the ones that match the regex
Here is a basic one that will match:
check out RegExr to build more regex.