简体   繁体   English

如何搜索属性值为“ \\”的元素?

[英]How to search for element with attribute value as “\”?

I have an element inside my html with attribute "test" having value "\\". 我的html内有一个元素,属性“ test”的值为“ \\”。 eg 例如

<e1>
 <e2 test="\"></e2>
</e1>

Now when I do a find to get the element as follows: elem.find('[value="\\"]') I get following error: 现在,当我进行如下查找以获取元素时:elem.find('[value =“ \\”]')我得到以下错误:

error: Syntax error, unrecognized expression: [value="\"]

Any ideas on how to fix this ? 有想法该怎么解决这个吗 ?

Corrected by @Karl-André Gagnon: it should be \\\\\\\\ and test= , not value= : 由@Karl-AndréGagnon更正:应该为\\\\\\\\并且test= ,而不是value=

$('e1').find('[test="\\\\"]');

Fiddle . 小提琴

In the documentation it says you need to add double slashes to escape a special character. 在文档中说您需要添加双斜杠以转义特殊字符。 But since \\ by itself is an escape character, it needs to be doubled up. 但是由于\\本身是转义字符,因此需要加倍。 As a result, you need to have 4 slashes to match a single slash. 因此,您需要有4个斜杠才能匹配单个斜杠。

$('[test="\\\\"]')

And I mentioned in my comment, you have no attribute value, it is test. 我在评论中提到,您没有属性值,这是测试。 And the jQuery documentation has a list of all the special characters in selectors that need to be escaped. jQuery文档列出了选择器中所有需要转义的特殊字符。

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

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