[英]Error: Syntax error, unrecognized expression
I was trying to trigger my onclick event until I got the following in console log: 我试图触发我的onclick事件,直到在控制台日志中得到以下信息:
Error: Syntax error, unrecognized expression: li[data-path='00's']
错误:语法错误,无法识别的表达式:li [data-path ='00's']
My code is as follows: 我的代码如下:
$( "li[data-path='00\'s']" ).on("click", function()
{
console.log("in 00s");
$('#replacewithimg').replaceWith('<img id="replacewithimg" src="../assets/img/playlist-icons/00s.png" style="padding-left: 5px;padding-right: 10px;padding-bottom: 5px;">');
$('#replacewithtext').replaceWith('<b id="replacewithtext" style="font-size:30px;">00\'s Playlist</b>');
$('#replacewithtext2').replaceWith('<p id="replacewithtext2" style="font-size:18px; padding-top:10px;">Includes Amy Whinehouse, Westlife, The Killers...</p>');
});
If you click on an element matching the li
tag which contains a data-path with 如果单击与
li
标签匹配的元素,其中li
标签包含带有
00's
00的
Then do its thing. 然后做它的事情。 I think is issue is with the escaping single quote?
我认为转义单引号是问题吗?
Use 采用
$( "li[data-path='00\\'s']" )
instead of 代替
$( "li[data-path='00\'s']" )
First backslash will escape the second backslash in function call send it to function and now the remaining backslash will be used by CSS for CSS selector 第一个反斜杠将在函数调用中转义第二个反斜杠,将其发送给函数,现在其余的反斜杠将由CSS用于CSS选择器
PS : Use this instead to avoid escaping the '
PS:请改用它以避免转义
'
$('#replacewithtext').replaceWith("<b id='replacewithtext' style='font-size:30px;'>00\'s Playlist</b>");
DEMO 演示
$( "li[data-path='00\\\\'s']" ).on("click", function() { console.log("in 00s"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li data-path="00's" >Click</li>
Here's some variations, probably best bet is to use a variable if you don't like using \\
: 这是一些变体,如果您不喜欢使用
\\
,则最好的选择是使用变量:
// with outer ', inner ", single \\ console.log($('div[data-path="00\\'s"]').length) // with outer ", inner ', double \\\\ console.log($("div[data-path='00\\\\'s']").length) // No \\, using a variable, with outer ' and inner " // This doesn't work inline as the js parser sees the ' // rather than it being a jquery limitation var path = "00's" console.log($('div[data-path="' + path + '"]').length) // without inner " or ' // With outer " use double \\\\ console.log($("div[data-path=00\\\\'s]").length) // With outer ' use triple \\\\\\ console.log($('div[data-path=00\\\\\\'s]').length) // doesn't work // console.log($("div[data-path=00's]").length) // console.log($("div[data-path=00\\'s]").length)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div data-path="00's"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.