语法错误,无法识别的href表达式 [英] Syntax error, unrecognized expression for href

查看:112
本文介绍了语法错误,无法识别的href表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加以下脚本并运行时.我得到这个:

When I add below script and run. I am getting this:

未捕获的错误:语法错误,无法识别的表达式:ul li a [href =#!id1]

Uncaught Error: Syntax error, unrecognized expression: ul li a[href=#!id1]

我不确定是哪个双引号引起了问题.

I am not sure which double quote causing the issue.

HTML

<ul>
 <li class="slist selected" id="id1"><a href="#!id10">Test1/a></li>
 <li class="slist" id="id2"><a href="#!id20">Test2</a></li>
 <li class="slist" id="id3"><a href="#!id30">Test3/a></li>
</ul>

JS

$(document).ready(function () {
    var id = "#!" + window.location.href.split("!")[1];
    if ($("ul li a[href=" + id + "]").length) {
        console.log("present");    
    } else {    
        console.log("absent")
    }
});

推荐答案

使用基于属性的选择器时,需要在引号中包含特殊字符.

You need to enclose special characters in quotes when using a attribute based selector.

if ($('ul li a[href="' + id + '"]').length) {


您选择器的版本将会出现


Your version of selector would result

if ($("ul li a[href=#!...]").length) {

#!将抛出无法识别的表达式.

The #! will throw unrecognized expression.

""转义字符的我的版本

if ($('ul li a[href="#!..."]').length) {

这篇关于语法错误,无法识别的href表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆