在jquery中使用转义字符串作为html元素id

Jou*_*man 3 html jquery

我通过转义文本短语来创建html元素ID,如下所示:

var elementid=escape('some term entered by user');
Run Code Online (Sandbox Code Playgroud)

结果,我有一个带有id的span,如下所示:

<span class="radio" id="selectTimescalesOne%20hello%20there" style="background-position: 0px 0px; "></span>
Run Code Online (Sandbox Code Playgroud)

如果我这样做:

$('#selectTimescalesOne%20hello%20there').html('some new stuff');
Run Code Online (Sandbox Code Playgroud)

然后跨度不会更新.使用转义字符串作为ids 有什么问题吗?

演示: http ://jsfiddle.net/uSwEJ/

Gol*_*rol 7

%在id中无效.您被限制为字母数字字符,_,-":"和.,虽然它是明智的,不要使用后三个,因为他们不能在CSS选择器中使用.

我真的不知道你为什么要用户输入你的id.好像很糟糕的设计.如果您确实想这样做,只需删除任何无效字符或用下划线替换它.