如何在jQuery选择器中转义括号? [英] How do you escape parentheses in jQuery selector?

查看:517
本文介绍了如何在jQuery选择器中转义括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查td内部文本是否包含括号().原因是我将负数显示为(1000),并且需要将它们转换为-1000才能进行数学运算.我尝试了几种不同的方法,但似乎无法正确解决.我知道有非jQuery的方法可以做到这一点,但目前这只是在烦我.

I am trying to check if a td innertext contains parentheses (). The reason is I display negative numbers as (1000) and I need to convert them to -1000 to do math. I've tried a couple different ways but can't seem to get it right. I know there are non-jQuery ways to do this but at this point it's just bugging me.

$(tdElement[i]).find("\\(").length > 0 

这不会引发错误,但是找不到(1000)的内部文本:

This doesn't throw error, but it doesn't find an innertext of (1000):

$(tdElement[i]).find("\\(")
{...}
    context: {object}
    jquery: "1.3.1"
    length: 0
    prevObject: {...}
    selector: "\("

我尝试过的另一种方法是:

Another method I tried was:

$("#fscaTotals td").filter(":contains('\\(')")

这将引发错误引发并未捕获异常".不过,它似乎也适用于其他角色.例子: . ,; < >

This throws error "Exception thrown and not caught". It seems to work for other characters though. Example: . , ; < >

那么,如何在jQuery中转义括号?

So, how do you escape parentheses in jQuery?

推荐答案

我认为您必须使用以下过滤器功能:

I think you'll have to use a filter function like:

$('#fscaTotals td *').filter(function(i, el) {
    return !!$(el).text().match(/\(/);
});

我认为这是jQuery :contains()中的错误.

I think this is a bug in jQuery's :contains().

这篇关于如何在jQuery选择器中转义括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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