Jquery为特殊字符id添加类
我有一个带有特殊字符的id,例如,
var aaa = "someCompany="some's Data"";
aaa = aaa.replace(/"/gi, "\"").replace(/'/gi, "\'").replace(/&/gi, "\&");
var abc = aaa.replace(/\"/gi, "\\\"").replace(/\:/gi, "\:").replace(/\'/gi,"\\\'");
var abcId = "li[id=\"" + abc + "\"]";
$(abcId).addClass(" sel");
在IE中无法为“abcId”“addClass”,但在firefox中工作正常。
谁能告诉我如何处理这些特殊字符...
提前致谢
I have a id with special characters like,
var aaa = "someCompany="some's Data"";
aaa = aaa.replace(/"/gi, "\"").replace(/'/gi, "\'").replace(/&/gi, "\&");
var abc = aaa.replace(/\"/gi, "\\\"").replace(/\:/gi, "\:").replace(/\'/gi,"\\\'");
var abcId = "li[id=\"" + abc + "\"]";
$(abcId).addClass(" sel");
In IE not able to "addClass" for "abcId" but working fine in firefox.
Could anyone please tell me how to handle these special characters...
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不完全确定你想要做什么,但你可能会考虑做一些便宜的事情,比如在写入时对 id 进行 URL 编码。然后您可以使用
$('#' + escape(theID))
通过 JQuery 获取它。之后,您可以使用$('#' + escape(theID)).addClass("sel")
添加您的类。为了进一步澄清,您可以使用 ID URL Encoded 写出您的 HTML,如下所示this:(大多数服务器技术可以通过简单的方法调用来做到这一点)
那么你的 javascript 将是这样的:
I'm not totally sure what you're trying to do, but you might think about doing something cheap like URL encoding the id as it's written. Then you can use
$('#' + escape(theID))
to get it with JQuery. After that you can add your class with$('#' + escape(theID)).addClass("sel")
To clarify further, you'd write out your HTML with the ID URL Encoded like this: (Most server technologies can do this with a simple method call)
Then your javascript would be like this:
为什么不尝试:
如果你有 li 的 ID,为什么还要使用选择器?
Why not try:
If you have the ID of the li, why are you using a selector?