簡體   English   中英

Jquery轉義選擇器無法識別的表達式:[data-route = search \\ / child]

[英]Jquery escaping selector unrecognized expression: [data-route=search\\/child]

我似乎無法找到解決這個問題的方法。

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

當路徑由一個部分#/搜索組成時,一切正常。 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 + "\"]";

你甚至不需要逃避/然后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM