我有一个 txtBox,它的 id 是:beginDateTxt
但是 jsf 使它 j_idt8:beginDateTxt
在 jquery 中,我尝试这样实现它
<script type="text/javascript">
$(document).ready(function() {
$(function() {
$("#j_idt8:beginDateTxt").mobiscroll().date({
theme: 'android-ics light', mode:'scroller', display: 'bottom'
});
});
});
</script>
但我得到以下错误:
Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: beginDateTxt
为什么?
你可以试试
$(document.getElementById('j_idt8:beginDateTxt')).mobiscroll().date({theme: 'android-ics light', mode:'scroller', display: 'bottom'});
通常 jQuery 在其 $()
函数中使用类似 CSS 选择器的东西。在 CSS 选择器中,:
表示一个伪类。但是,在您的情况下,:
只是 id 的一部分。
如果您使用通用 getElementById()
,参数不会被分解,而是被视为一个 ID。因此,通过使用 getElementById()
并用 $()
包装结果,您可以避免这种“误解”。
但是,总的来说,我认为最好更改 JSF 中的命名空间方案。
编辑
jQuery documentation on selectors声明您应该使用 \\
转义特殊字符:
To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \. For example, an element with id="foo.bar"
, can use the selector $("#foo\\.bar")
.
这将导致大牛已经给出的答案,我认为它优于上面给出的答案。然而,这种解释仍然有效。
$("#j_idt8\\:beginDateTxt").mobiscroll().date({theme: 'android-ics light', mode:'scroller', display: 'bottom'});