twitter-bootstrap Bootstrap 下拉菜单 Jquery Uncaught Error: Syntax error, unrecognized expression
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35539374/
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
Bootstrap dropdown Jquery Uncaught Error: Syntax error, unrecognized expression
提问by Joseph Raphael
I am trying to add an external http:// link on my bootstrap drop-down menu, but it always cause an
我试图在我的引导下拉菜单中添加一个外部 http:// 链接,但它总是导致
Uncaught Error: Syntax error, unrecognized expression: http://example.com/
Here's the code I used with jQuery v1.11.3 and bootstrap v3.3.5
这是我在 jQuery v1.11.3 和 bootstrap v3.3.5 中使用的代码
<nav class="main-navi">
<div class="webinarlink"><a class="btn btn-danger btn-block" data-target="#" style="background-color:#EA4A2B;margin:auto;" href="http://example.com/" target="_blank"><i class="icon-users"></i>Webinar</a></div>
<ul>
<li class="active"><a class="scroll-up" href="#home"><i class="fa fa-home"></i> Home</a></li>
<li class=""><a class="scroll" href="#features">Features</a></li>
<li class=""><a class="scroll" href="#testimonials">Testimonials</a></li>
<li class=""><a class="scroll" href="#pricing">Prices</a></li>
<li class="has-dropdown"><a class="scroll" href="#">Help<i class="fa fa-caret-down"></i></a>
<ul class="dropdown">
<li class=""><a class="scroll" href="#faq"><i class="fa fa-question-circle"></i> FAQ</a></li>
<li class=""><a class="scroll" href="#team"><i class="fa fa-info-circle"></i> About Us</a></li>
<li class=""><a class="scroll" href="#contus"><i class="fa fa-envelope"></i> Contact Us</a></li>
<li class=""><a href="#pricing" class="scroll" onclick="Tawk_API.toggle();"><i class="fa fa-comments"></i> Chat with Us</a></li>
</ul>
</li>
</ul>
</nav>
I tried adding data-target="#" but it didn't work.
我尝试添加 data-target="#" 但它没有用。
采纳答案by Joseph Raphael
The issue has been fixed by following these steps:
该问题已通过以下步骤修复:
- Use latest version of jQuery v2.1.3 and bootstrap v3.3.6
- Load Bootstrap JS after jquery in order
- Using data-target="#" with href="#" and put the external link on an onClick() trigger.
- 使用最新版本的 jQuery v2.1.3 和 bootstrap v3.3.6
- 在 jquery 之后按顺序加载 Bootstrap JS
- 使用 data-target="#" 和 href="#" 并将外部链接放在 onClick() 触发器上。
Hope it helps.
希望能帮助到你。
回答by Nux
If you are stuck on old Bootstrap then you can patch getParentfunction of bootstrap-dropdown.js.
如果你是卡在旧引导,那么你可以修补getParent的功能bootstrap-dropdown.js。
Just replace $parent = selector && $(selector)with this:
只需替换$parent = selector && $(selector)为:
if (selector && selector !== '#') {
$parent = $(selector)
}
回答by Ayoub Laazazi
In my case, I had a script that automatically switches the "active" class on menu items on scroll, all the 'href' of the 'a' html tags under the Boostrap menu must not equal to "#", this solution worked for my case, if anyone has a similar case.
就我而言,我有一个脚本可以在滚动时自动切换菜单项上的“活动”类,Boostrap 菜单下“a”html 标签的所有“href”不得等于“#”,此解决方案适用于我的情况,如果有人有类似的情况。
回答by diego cantero
Here is my solution to the getParent function in bootstrap
这是我对 bootstrap 中 getParent 函数的解决方案
function getParent($this) {
var selector = $this.attr('data-target')
if (!selector) {
selector = $this.attr('href')
selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
}
//var $parent = selector && $(selector)
var $parent = null;
if (selector && selector !== '#') {
$parent = $(selector)
}
return $parent && $parent.length ? $parent : $this.parent()
}
回答by user3230982
Please check JQuery added in your page. Mostly this type of error comes due to 2 or more jquery versions
请检查在您的页面中添加的 JQuery。这种类型的错误主要是由于 2 个或更多 jquery 版本引起的

