元素的数量不正常

时间:2014-05-05 09:40:59

标签: jquery

我正在创建路径元素并添加到组中。在这里,我不想创建和添加具有相同ID的路径到组。所以我正在跳过创建和添加路径,如果“id”之前使用如下

drawPath: function (options, groupElement) {
        if ($("#" + options.id).length > 0) { 
          break;
        }
        else {
            var path = document.createElementNS(this.svgLink, "path");
            $(path).attr(options).appendTo(element);
        }

    },


here the options is the attribute to be set for the path and element is the group element to which the path is added  



options = {
                'id': '23+',
                'd': 'M 0 2 L 3 5'
            };

每个路径的选项都不同。

当id为'23 +'或'23 Above'时,$(“#”+ options.id)。length总是显示0,即使第二路径id为'23 +'。但我不想添加多个具有相同ID的路径。

I am getting this problem only when the id is having special character and white space.

我在哪里做错了

提前致谢

1 个答案:

答案 0 :(得分:3)

#23+不会被解析为jquery选择器,因为+是一个元字符。你必须转义那个+符号才能使它成为一个有效的选择器。

尝试改变你的json,

options = {
           'id': '23\\+',
           'd': 'M 0 2 L 3 5'
          };

请阅读 here 以了解jquery中的元字符以及如何将其用作选择器。

相关问题