jQuery:无法识别的表达式 [英] jQuery: unrecognized expression
本文介绍了jQuery:无法识别的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$(document).ready(function(){
var page = window.location.hash;
if(page != ""){
$('a[href='+ page +']').addclass('selected');
pageload(page.replace('#/page/', 'pages/?load='));
}
$('#top a').click(function(event){
$('#top a').removeClass('selected');
$(this).addClass('selected');
pageload($(this).attr('href').replace('#/page/', 'pages/?load='));
event.preventDefault;
});
});
<div id="top">
<a href="#/page/link">Link</a>
<a href="#/page/link">Link</a>
<a href="#/page/link">Link</a>
<a href="#/page/link">Link</a>
<a href="#/page/link">Link</a>
</div>
所以当我试图这样做,并使用window.location加载页面。哈希,我在控制台中出现错误说:
So when i'm trying to do this, and load up a page using the window.location.hash, i get an error in the console saying:
未捕获错误:语法错误,无法识别的表达式:
[href =# / page / link]
Uncaught Error: Syntax error, unrecognized expression: [href=#/page/link]
我该如何使这项工作?
How can i make this work?
谢谢
推荐答案
b
$ b
Try this instead:
$('a[href="'+ page +'"]').addClass('selected');
(您需要转义 href
- 这样,你得到 a [href =#/ page / link]
。)
(You need to escape the value of the href
– with this, you get a[href="#/page/link"]
.)
这篇关于jQuery:无法识别的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文