限于jQuery id字符串? [英] Limit to jQuery id strings?
问题描述
hr>
更新:我得到了ID部分,但并不是为什么我甚至不能使用该字符串搜索html内容。任何人都愿意告诉我一个正则表达式,将模式从MM / dd / yy HH:mm:ss更改为MMddyyHHmmss
详细信息如下:
我试图获取多个< p>
s使用jQuery。
添加它们的方式是使用以下代码:
$(< p> + data.timeStamp +_+ data.text +< / p>)
.addClass(data。来源)
.attr(id,msg _+ data.userId +_+ data.timeStamp)
.appendTo($(#messages _+ data.userId));
他们看起来很好,我可以添加尽可能多的。但是,我需要获取每个中的文本才能附加响应。我使用 userId
和 timeStamp
来找到我需要追加的段落。如果 timeStamp
只是一个数字(例如,由java servlet创建为
SimpleDateFormat df = new SimpleDateFormat(MMddyyHHmmss);
但jQuery无法如果timeStamp有点复杂的话,可以找到它:
SimpleDateFormat df = new SimpleDateFormat(MM / dd / yy HH: mm:ss);
甚至
SimpleDateFormat df = new SimpleDateFormat(MM-dd -yy HH.mm.ss);
我试过了两种方法:
var txt = $(#msg _+ data.message .from +_+ data.message.timeStamp).html();
和
var txt = $(#messages _+ data.userId).find(:contains('+ data.message.timeStamp + '))。html();
无济于事。 b
JSON在ajax调用期间被编码和解码,因为 console.log(response.ti meStamp);
会给出正确的输出。
任何想法?我应该逃避 timeStamp
字符串以在jQuery搜索中使用它?
PS 1: / strong>如果您发现任何语法错误,最有可能是由于剪切&粘贴和简化代码的例子,因为用一个简单的timeStamp我可以使它在真正的js文件中工作。
PS 2:我知道,在我的具体情况下,解决方法是使用两个不同的字符串,一个用于文本中的时间戳,另一个用于标识(简单),但这并不能帮助我理解这里的限制。
您需要为您的元素ID使用有效的值。从html 4规范:
ID和名称标记必须以字母([A-Za-z])开头, ([0-9]),连字符( - ),下划线(_),冒号(:)和句点(。)。
但是当你在ID中使用冒号和句点时,你会遇到jQuery css选择器的问题,因为它们被jQuery解析为类名和伪元素。所以你的元素ID只能包含字母,数字,连字符和下划线。
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 complicated:
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.
You 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.
这篇关于限于jQuery id字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!