简体   繁体   English

如何在jquery或javascript中选择ID包含\\的元素

[英]How to select element with id containing \ in jquery or javascript

we have some legacy code where Html element ID's are populated dynamically from database data (I cant change the data here) ex. 我们有一些旧代码,其中从数据库数据动态填充HTML元素ID(在这里我不能更改数据)。 <input type="text" id="2314/test/film/code\\branch"/>

when I get the ID from click event like below var src = window.event.srcElement; 当我从如下所示的click事件获取ID时var src = window.event.srcElement; I get src.id= "2314/test/film/code\\\\branch"; 我得到src.id= "2314/test/film/code\\\\branch";

I want to use the src.id to find the same element in different function like $(_element).find("[id='" + src.id + "']").get(); 我想使用src.idlike $(_element).find("[id='" + src.id + "']").get();不同函数中查找相同元素like $(_element).find("[id='" + src.id + "']").get();

which is failing to get any ID since I see "\\" is replaced with "\\\\" 由于我看到"\\"被替换为"\\\\" ,因此无法获取任何ID

Please suggest me how to get around this ? 请建议我如何解决这个问题?

I don't think jQuery lets you use / in a selector. 我不认为jQuery允许您在选择器中使用/ It is an illegal character in an id name, but still odd that jquery seems to flat out refuse it. ID名称中的非法字符,但jquery似乎完全拒绝它仍然很奇怪。 Since JS has no problem with that selector You can select it with JS then pass it off to the jquery wrapper. 由于JS对该选择器没有问题,因此可以使用JS选择它,然后将其传递给jquery包装器。

$(document.getElementById('2314/test/film/code\\branch'));

Maybe it´s works for you 也许对你有用

http://codepen.io/luarmr/pen/vOgoLO http://codepen.io/luarmr/pen/vOgoLO

$("[id='" + str.replace(/\\/g,'\\\\')  + "']").html(str)

反斜杠的正确正则表达式为'\\\\\\\\'。

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

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