ID中带有$的元素不能在jquery中更改 [英] Elements with $ in the id can't be altered in jquery

查看:43
本文介绍了ID中带有$的元素不能在jquery中更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成的文件,有些ID包含$个字符.如果其中包含$字符,则JQuery无法使用该元素.无论如何要解决此问题而不更改ID?

I've got a generated file and some IDs contain $ characters. JQuery can't work with the element if it has a $ character in it. Anyway to go around this without changing the IDs?

示例: https://jsfiddle.net/duLba02y/

<div id="test">This works.</div>
<div id="te$t">This doesn't</div>
<script>
   $("#test").hide();
   $("#te$t").hide();
</script>

推荐答案

由于$是元字符,因此请使用\\对其进行转义.

Since $ is a meta characters, use \\ to escape it.

$("#te\\$t").hide();

文档

使用任何元字符(例如!#$%&'()* +,./:;< =>?@ [] ^`{|}〜)作为文字部分名称,必须使用两个反斜杠将其转义:\\.

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\.

这篇关于ID中带有$的元素不能在jquery中更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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