JQuery: unrecognized expression

$(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> 

Therefore, when I try to do this and load the page using window.location.hash, I get an error message in the console:

Search error: syntax error, unrecognized expression: [href = # / page / link]

How can I do this job?

thanks

+7
source share
2 answers

Try this instead:

 $('a[href="'+ page +'"]').addClass('selected'); 

(You need to avoid the href value - with this, you will get a[href="#/page/link"] .)

+16
source

Your regular expression does not need voice tags:

 replace(#/page/, ... 
0
source

All Articles