2014-10-28 55 views
3

我似乎找不到解決此問題的解決方法。jquery轉義選擇器無法識別的表達式:[data-route = search \/child]

在一個SPA應用程序中,我有不同的路線。 #/ search#/ other#/ search/child。我解析hashtag並顯示/隱藏包含屬性data-route的元素。

當路徑由一個部分#/搜索組成時,一切正常。 Jquery正在做$('[data-route = search]')。

當我添加第二個部分#/搜索/孩子,我逃選擇進入「搜索\ /子」,但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); 
} 
+0

嘗試'[data-route =「search/child」]','[data-route =「search \/child」]','[data-route =「search \\/child」]' 。 – amphetamachine 2014-10-28 22:12:55

回答

4

嘗試使用引號。

var selector = "[data-route=\"" + route + "\"]"; 

然後你甚至不需要轉義/

+0

工作正常!謝謝! – sesteva 2014-10-29 02:58:15

相關問題