我似乎找不到解决这个问题的方法。
在SPA应用程序中,我有不同的路线#/search#/other#/search/child。我解析这个标签并显示/隐藏一个包含属性数据路由的元素。
当路径由一个部分组成时,#/search一切都很好。Jquery正在执行$('[data-route=search]')。
当我添加第二部分#/search/child时,我将选择器转义为"search\/child",但Jquery无法运行选择器,并出现以下错误消息:
Uncaught Error: Syntax error, unrecognized expression: [data-route=search\/child] jquery.js:1850Sizzle.error jquery.js:1850tokenize jquery.js:2460select jquery.js:2847Sizzle jquery.js:1289jQuery.fn.extend.find jquery.js:5730jQuery.fn.jQuery.init jquery.js:197jQuery jquery.js:63allroutes MainView.js:21apply director.js:511_every director.js:308Router.invoke director.js:516updateAndInvoke director.js:474Router.dispatch director.js:482handler director.js:203onchange director.js:76
如果我打开控制台并执行相同的选择器,它工作得很好。
var route = window.location.hash.slice(2);
route = route.replace('/','\\/');
var sections = $('[data-route]');
var selector = "[data-route=" + route + "]";
var section = $(selector);
if (section.length) {
sections.hide(250);
section.show(250);
}
尝试使用引号。
var selector = "[data-route="" + route + ""]";
那么你甚至不需要逃离/
。