admin管理员组文章数量:1323354
In just a few words, my question is what are the limitations for a string to be a searchable id or searchable content in jQuery.
Update: I got the ID part, but not why I can't even search in the html content with that string. A bouns for anyone willing to tell me a regex to change the pattern from "MM/dd/yy HH:mm:ss" to "MMddyyHHmmss"
Details follow:
I'm trying to obtain the text inside multiple <p>
s using jQuery.
The way I'm adding them is with the following code:
$("<p>"+data.timeStamp + " _ " + data.text +"</p>")
.addClass(data.source)
.attr("id", "msg_"+data.userId+"_"+data.timeStamp)
.appendTo($("#messages_"+data.userId));
They look fine and I can add as many as I can. But then, I need to obtain the text inside each one in order to append a response. I use the userId
and the timeStamp
to find which paragraph I need to append to. It works just fine if timeStamp
is just a number (e.g., created by the java servlet as
SimpleDateFormat df = new SimpleDateFormat("MMddyyHHmmss");
but jQuery is unable to find it if the timeStamp is a little more plicated:
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
or even
SimpleDateFormat df = new SimpleDateFormat("MM-dd-yy HH.mm.ss");
I tried both
var txt = $("#msg_"+data.message.from+"_"+data.message.timeStamp).html();
and
var txt = $("#messages_"+data.userId).find(":contains('"+data.message.timeStamp+"')").html();
to no avail.
The JSON is encoded and decoded fine during the ajax call, since console.log (response.timeStamp);
will give the right output.
Any ideas? Should I escape somehow the timeStamp
string to use it in the jQuery search?
PS 1: If you find any syntax error, it is most probably due to the cut & paste and simplification of the code for the example, since with a simple timeStamp I can make it work in the real js file.
PS 2: I know that a workaround in my specific case would be to use two different strings, one for the time stamp in the text and one (simpler) for the id, but that does not help me understand the limitation here.
In just a few words, my question is what are the limitations for a string to be a searchable id or searchable content in jQuery.
Update: I got the ID part, but not why I can't even search in the html content with that string. A bouns for anyone willing to tell me a regex to change the pattern from "MM/dd/yy HH:mm:ss" to "MMddyyHHmmss"
Details follow:
I'm trying to obtain the text inside multiple <p>
s using jQuery.
The way I'm adding them is with the following code:
$("<p>"+data.timeStamp + " _ " + data.text +"</p>")
.addClass(data.source)
.attr("id", "msg_"+data.userId+"_"+data.timeStamp)
.appendTo($("#messages_"+data.userId));
They look fine and I can add as many as I can. But then, I need to obtain the text inside each one in order to append a response. I use the userId
and the timeStamp
to find which paragraph I need to append to. It works just fine if timeStamp
is just a number (e.g., created by the java servlet as
SimpleDateFormat df = new SimpleDateFormat("MMddyyHHmmss");
but jQuery is unable to find it if the timeStamp is a little more plicated:
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
or even
SimpleDateFormat df = new SimpleDateFormat("MM-dd-yy HH.mm.ss");
I tried both
var txt = $("#msg_"+data.message.from+"_"+data.message.timeStamp).html();
and
var txt = $("#messages_"+data.userId).find(":contains('"+data.message.timeStamp+"')").html();
to no avail.
The JSON is encoded and decoded fine during the ajax call, since console.log (response.timeStamp);
will give the right output.
Any ideas? Should I escape somehow the timeStamp
string to use it in the jQuery search?
PS 1: If you find any syntax error, it is most probably due to the cut & paste and simplification of the code for the example, since with a simple timeStamp I can make it work in the real js file.
PS 2: I know that a workaround in my specific case would be to use two different strings, one for the time stamp in the text and one (simpler) for the id, but that does not help me understand the limitation here.
Share Improve this question edited Apr 11, 2011 at 11:29 Aleadam asked Apr 11, 2011 at 10:23 AleadamAleadam 40.4k9 gold badges88 silver badges108 bronze badges 2- 1 I think IDs are not allowed to contain spaces. – Felix Kling Commented Apr 11, 2011 at 10:28
- @Felix got that, but I can't even find that string searching in the content – Aleadam Commented Apr 11, 2011 at 11:30
3 Answers
Reset to default 8You need to have a valid value for your element IDs. From the html 4 spec:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
But you will propably have problems with jQuery css selectors when using colons and periods in your IDs, since these are parsed by jQuery as class names and pseudo-elements.
So your element IDs should only contain letters, digits, hyphens and underscores.
Based on the ments and the answer, I tried to escape with a double backslash the string, but it didn't work (still no results), even when searching the text and not using those long IDs. So, I ended up removing spaces and symbols on the id part by doing on the client:
var ts = data.timeStamp.replace(/[\:\/ ]/g, "");
and on the server:
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
Then I added the formatted date to the text and the stripped down to the ID. It works, though I am still curious about what the limitations are for jQuery search strings.
I'm a little late to this but I came across some jquery selector limits. I tried Math.random() with no joy. Then I added a "uid:" to the start without luck but suspect JQ thinks the colon is 'something'. Finally I coerced the number to a six-digit positive interger and prepended "uid_".
Success! I don't know if length or one of [.:] were the issue but whatever the format limits are should be documented somewhere.
本文标签: javascriptLimit to jQuery id stringsStack Overflow
版权声明:本文标题:javascript - Limit to jQuery id strings? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742129580a2422096.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论