未捕获的错误:语法错误,无法识别的表达式:#/ angularjs 和 jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31498325/
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-08-26 16:48:23  来源:igfitidea点击:

Uncaught Error: Syntax error, unrecognized expression: #/ angularjs and jquery

jqueryangularjstwitter-bootstrap

提问by ram

Following error shown up in my console whenever my page loads.

每当我的页面加载时,我的控制台中都会显示以下错误。

"Uncaught Error: Syntax error, unrecognized expression: #/about"

“未捕获的错误:语法错误,无法识别的表达式:#/about”

My code is as below:

我的代码如下:

                <li class="active">
                    <a href="/">Home</a>
                </li>
                <li class="">
                    <a href="#/about" title="About Us">About</a>
                </li>
                <li class="">
                    <a href="#/pricing">Pricing</a>
                </li>

It shows for all initial instance of . Initially, it shows for #/about as it is first, If I remove about us tab it will show up for pricing page link.

它显示了 的所有初始实例。最初,它首先显示 #/about,如果我删除 about us 选项卡,它将显示定价页面链接。

Its not bootstrap tab issue, This are simple navigation only

它不是引导标签问题,这只是简单的导航

Routing Code in app.js:

app.js 中的路由代码:

$routeProvider.when('/', {
        templateUrl: 'partials/home.html'
    }).when('/account', {
        templateUrl: 'partials/account.html',
    }).when('/terms', {
        templateUrl: 'partials/terms.html'
    }).when('/about', {
        templateUrl: 'partials/about.html'
    }).otherwise({
        redirectTo: '/'
    });

I have debug its issue of bootstrap, bootstrap nav causing this issue: "nav navbar-nav" fixes for this is data-target="#" but wants proper work around for this

我已经调试了它的引导程序问题,引导程序导航导致了这个问题:“nav navbar-nav”对此的修复是 data-target="#" 但希望对此进行适当的解决

回答by Bidhan

This is most probably a Bootstrap issue. Try using data-targetattribute on your links like this

这很可能是 Bootstrap 问题。尝试data-target在您的链接上使用这样的属性

<a href="#/about" data-target="#about" title="About Us">About</a>

回答by Sudhansu Choudhary

Ok you gotta remove the slash, '/' with which your html becomes,

好的,你必须删除斜杠,“ /”,你的 html 变成了,

<a href="#about" title="About Us">About</a>

If you want to keep the slash, '/' you gotta use data-targetattribute as,

如果你想保留斜线,' /' 你必须使用data-target属性作为,

<a href="#/about" title="About Us" data-target="#about">About</a>

More about it here

更多关于这里

回答by Miguel Peguero

This issue happens also on angular 2 projects. I just add data-target="#" to the anchor link. It solved my problem.

这个问题也发生在 angular 2 项目上。我只是将 data-target="#" 添加到锚链接。它解决了我的问题。

 <a data-target="#" [routerLink]="['/link']">my link</a>. 

回答by Vagrant326

This may be a very rare situation, but I've got same error because of some legacy code using Colorbox and location.hash.

这可能是一种非常罕见的情况,但由于一些使用 Colorbox 和location.hash.

jQuery(function() {jQuery('.content-row .csc-default a').tooltip({placement:'top'});
jQuery('a.gallery').colorbox({
    maxWidth:'95%',
    maxHeight:'95%',
    slideshow:true,
    current:' {current} / {total}',
    opacity:'0.80',
    transition:'none',
    speed:'550',
    slideshowSpeed:'5500',
    overlayClose:true,
    fixed:false,
    escKey:true,
    arrowKey:true,
    loop:true,
    title: function() { return $(this).data('original-title')},
    close:'<span class="glyphicon glyphicon-remove"></span>',
    previous:'<span class="glyphicon glyphicon-chevron-left"></span>',
    next:'<span class="glyphicon glyphicon-chevron-right"></span>',
    slideshowStart:'<span class="glyphicon glyphicon-play"></span>',
    slideshowStop:'<span class="glyphicon glyphicon-pause"></span>',
    rel: function() { return $(this).data('rel')}
});
if (location.hash) $(location.hash).collapse('show'); //when this line is commented, Angular Route works properly
});

Strange thing is that error was happening only when loading page with link to view like localhost/index.html#/mainor simply reloading page with any view selected.

奇怪的是,只有在加载带有链接的页面时才会发生错误,localhost/index.html#/main或者只是在选择了任何视图的情况下重新加载页面。