jQuery 升级后,未捕获异常:语法错误,无法识别的表达式:h3
我正在开发该网站的新版本,现在发现我的 javascript 代码存在一个问题,该问题没有得到很好的处理。
我过去一直在做的是
$('#lb_outer_title :h3').html(title);
更改灯箱标题属性的文本。这在 jQuery 1.3.2 中工作得很好,但是在新版本(使用 jQuery 1.4.2)上,我收到错误:
uncaught exception: Syntax error, unrecognized expression: Syntax error, unrecognized expression: h3
在新版本中,我可以将代码更改为以下内容并且它可以工作,但这不能是最佳解决方案:
$('#lb_outer_title').html('<h3>'+ title + '</h3>');
我已经在新版本中尝试使用 :first 选择器,但这也无法按预期工作。
这是我正在使用的 html(注意:我无法将 class 或 id 添加到 h3 标记):
<div id="lb_outer_title">
<h3>Title</h3>
</div>
I'm working on a new version of the website, and am now finding an issue with my javascript code that isn't being handled nicely.
What I have been doing in the past is
$('#lb_outer_title :h3').html(title);
which changed the text of the title attribute for my lightbox. This worked fine in jQuery 1.3.2, but on the new version (which is using jQuery 1.4.2) I get the error:
uncaught exception: Syntax error, unrecognized expression: Syntax error, unrecognized expression: h3
In the new version I can change the code to the following and it works, but this can't be the optimal solution:
$('#lb_outer_title').html('<h3>'+ title + '</h3>');
I've tried the using :first selector in my new version, but that doesn't work as desired either.
Here is the html that I'm working with (note: I can't add class or id to the h3 tag):
<div id="lb_outer_title">
<h3>Title</h3>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的错误在这里:
h3
之前的冒号应该被删除。 jQuery 1.4 有一个更严格的选择器引擎,这就是它报告错误的原因(因为您在元素选择器上使用伪类前缀),而 jQuery 1.3 只是默默地解析掉冒号。Your mistake is here:
That colon before the
h3
should be dropped. jQuery 1.4 has a stricter selector engine and that's why it's reporting your error (since you're using a pseudo-class prefix on an element selector), while jQuery 1.3 merely silently parses the colon away.不确定那个冒号在那里做什么...它不是一个有效的选择器。
:
用于伪类,但您只需要一个元素。Not sure what that colon was doing in there... It is not a valid selector.
:
is used for psuedo-classes, but you simply want an element.只需删除
:
即可,如下所示:冒号前缀
:
用于各种 伪选择器,您只需要h3
,即 元素选择器.Just remove the
:
like this:The colon prefix,
:
, is used for various pseudo selectors, you just wanth3
, the element selector.