javascript 未捕获的错误:语法错误,无法识别的表达式:不支持的伪:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16077280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:12:02  来源:igfitidea点击:

Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo:

javascriptjsf

提问by user2294016

i have an txtBox and its id is : beginDateTxt

我有一个 txtBox,它的 id 是:beginDateTxt

but jsf makes it j_idt8:beginDateTxt

但是 jsf 做到了 j_idt8:beginDateTxt

in jquery i try to reach it like that

在 jquery 中,我尝试像那样到达它

  <script type="text/javascript">
            $(document).ready(function() {
                $(function() {
                    $("#j_idt8:beginDateTxt").mobiscroll().date({
                       theme: 'android-ics light', mode:'scroller', display: 'bottom'
                    });
                });

            });
   </script>

but i get below error:

但我得到以下错误:

Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: beginDateTxt

未捕获的错误:语法错误,无法识别的表达式:不受支持的伪代码:beginDateTxt

why?

为什么?

回答by Sirko

You could try

你可以试试

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

In general jQuery uses something like CSS selectors in its $()function. In a CSS selector the :denotes a pseudo-class. However, in your case the :is just a part of the id.

一般来说,jQuery 在其$()功能中使用类似 CSS 选择器的东西。在 CSS 选择器中,:表示伪类。但是,在您的情况下,:这只是 id 的一部分。

If you use the generic getElementById(), the argument is notdecomposed, but seen as an ID altogether. So by using getElementById()and wrapping the result with $()you can circumvent this "misunderstanding".

如果使用 generic getElementById(),则参数不会分解,而是完全视为 ID。因此,通过使用getElementById()和包装结果,$()您可以规避这种“误解”。

In general, however, I think it would be better to change the namespacing scheme in your JSF.

但是,总的来说,我认为最好更改 JSF 中的命名空间方案。

EDIT

编辑

The jQuery documentation on selectorsstates that you should escape special characters by the use of \\:

关于选择器jQuery 文档指出,您应该使用以下方法转义特殊字符\\

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").

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

This will lead to the answer already given by Daniel, which in my opinion is superior to the answer given above. The explanation, however, remains valid.

这将导致丹尼尔已经给出的答案,在我看来它优于上面给出的答案。然而,解释仍然有效。

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

回答by Daniel

If you want to use jQuery id selector you need to escape the :with \and then to escape the \(double escape)

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

Here:

这里:

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