5

我可以在 jquery mobile 中"("使用id 吗?")"

例如:

<div id="RSI(3,4)">
</div>

我已经尝试过该 id 并想在 js 中访问该元素。

$("#RSI(3,4)").html("some text");

但它没有奏效。

4

3 回答 3

5

转义特殊字符

$("#RSI\\(3\\,4\\)").html("some text");

http://api.jquery.com/category/selectors/

要使用任何元字符(例如 !"#$%&'()*+,./:;<=>?@[\]^`{|}~ )作为名称的文字部分,它必须用两个反斜杠转义:\\。例如,id="foo.bar" 的元素可以使用选择器 $("#foo\\.bar")。

也就是说,关于在 ID 中使用特殊字符的有效性,一些验证器会抱怨,我当然不建议在 ID 中使用任何特殊字符,也不建议使用前导数字,因为您可以根据最新的 html 规范。让您的生活更轻松,仅使用 a-zA-Z0-9,如果需要,请下划线

于 2013-09-10T12:40:18.617 回答
1

你能检查一下这个 jsfiddle吗

$(document).ready(function() {
$("#RSI\\(3\\,4\\)").html("some text");
});

工作正常

于 2013-09-10T12:49:11.793 回答
1

W3C Validator 说这个 html 代码

<html>
<head>
    <title>TITLE</title>
</head>
<body>
    <div id="RSI(3,4)"></div>
</body>
</html>

对 html5 有效,但对 html4.01 Strict 无效,因为它(id属性中的无效字符。 http://validator.w3.org/check

在 jQuery 中,您必须使用\\

http://api.jquery.com/category/selectors/

于 2013-09-10T12:43:56.363 回答