标记字符串包含点时的Jquery。当硬编码时,查询获得期望值,但如果使用函数获取标记并连接,则查询失败。
var tagWithDot = getTag(...) // tagWithDot === 'tag.withdot'
console.log(tagWithDot === 'tag.withdot') // true
console.log('#' + tagWithDot === '#tag.withdot') // true
console.log('#' + tagWithDot.replace('.', '\\.') === '#tag\\.withdot') // true
console.log($('#' + tagWithDot.replace('.', '\\.')) === $('#tag\\.withdot')) // false
console.log($(('#' + tagWithDot.replace('.', '\\.'))) === $('#tag\\.withdot')) // false
答案 0 :(得分:0)
.
内容替换手动\\.
var tagWithDot = "#tag.withdot";
console.log( $.escapeSelector(tagWithDot) );

<script src="//code.jquery.com/jquery-3.1.0.js"></script>
&#13;
答案 1 :(得分:0)
使用replace('.','\\.')
似乎在这里工作。
请注意,如果您使用$.escapeSelector
,则不会包含#或#scart也会被转义,并且可能不是您想要的。
var tagWithDot = "#tagwith.dot";
setTimeout(function () {
$(tagWithDot.replace('.','\\.')).text('Replaced');
}, 2000);
<script src="//code.jquery.com/jquery-3.1.0.js"></script>
<div>Wait 2 seconds and replace text in div with id #tagwith.dot<div>
<br>
<div id="tagwith.dot">This should get replaced</div>