未捕获错误:语法错误,无法识别的表达式:unsupported pseudo:

时间:2013-04-18 07:46:57

标签: javascript jsf

我有一个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>

但我得到以下错误:

  

未捕获错误:语法错误,无法识别的表达式:unsupported pseudo:beginDateTxt

为什么?

2 个答案:

答案 0 :(得分:60)

你可以尝试

$(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表示您应该使用\\转义特殊字符:

  

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

这将导致丹尼尔已经给出的答案,我认为这个答案优于上面给出的答案。但是,解释仍然有效。

$("#j_idt8\\:beginDateTxt").mobiscroll().date({theme: 'android-ics light', mode:'scroller', display: 'bottom'});

答案 1 :(得分:30)

如果你想使用jQuery id选择器,你需要使用:转义\然后转义\(双重转义)

下面:

$(function() {
    $("#j_idt8\\:beginDateTxt").mobiscroll().date({
        theme: 'android-ics light',
        mode:'scroller', display: 'bottom'
    });
});