当id包含逗号时,jQuery get元素不起作用 [英] Jquery get element not working when id contains comma

查看:113
本文介绍了当id包含逗号时,jQuery get元素不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当id包含逗号时,为什么jquery选择器不起作用?参见 http://jsfiddle.net/sGQas/188/

Why does the jquery selector not work when the id contains a comma? See http://jsfiddle.net/sGQas/188/

<div id="1,3">Hello There!</div>

$(document).ready(function () {
  var str = "#1,3";

  if ($(str).length) {
    alert($(str).position().left);
  } else {
    alert('The element "'+str+'" does not exist on the document!');
  }
});

推荐答案

您需要使用两个反斜杠对逗号进行转义:

You need to escape the comma with two backslashes:

var str = "#1\\,3";

jsFiddle示例

jsFiddle example

请参见 https://api.jquery.com/category/selectors/

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

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 get元素不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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