简体   繁体   English

$(“'” + foo +“'”)与$(foo)

[英]$(“'” + foo + “'”) vs $(foo)

I have some code that has been working since it's beginning, well over a two months. 自开始以来,我有一些代码已经工作了两个多月。 The selectors had dynamic content, stored in the variable newClasses . 选择器具有动态内容,存储在变量newClasses My code used to be $("'" + newClasses + "'") , and about a two days ago, after I added separate functionality on the page, not effecting this code, and not importing any libraries or anything else, I got the error of 我的代码以前是$("'" + newClasses + "'") ,大约两天前,在页面上添加了单独的功能,不影响此代码,并且未导入任何库或其他东西之后,我得到了错误

Uncaught Error: Syntax error, unrecognized expression: '.fs.btn.heading.abstract' code.jquery.com/jquery.min.js:2

AFTER a day of troubleshooting, I found that if i removed the "'" + and + "'" , and it became $(newClasses) , the functionality started working again. 经过一天的故障排除后,我发现如果删除了"'" ++ "'"并变成$(newClasses) ,该功能将重新开始起作用。 FYI: the variable newClasses did not include ' s on either end of the string. FYI:变量newClasses不包括' S对于字符串的任一端。

Now, I know this is somewhat of a different question, but what code could be added in a separate section to effect the functionality, and result in my having to remove the concatenation of the single quotes? 现在,我知道这是一个不同的问题,但是可以在单独的部分中添加哪些代码来实现功能,并导致我不得不删除单引号的串联?

Notes - 注意事项-

  • I am not looking for code help to the webpage, more troubleshooting methodology and thinking answers. 我不是在寻找网页的代码帮助,更多的故障排除方法和思考的答案。
  • due to the volume of code, and the different code that was added, it is pointless to post the code, but you can feel free to review the Git code here 由于代码量大,加上所添加的代码不同,因此发布代码毫无意义,但是您可以在此处随意查看Git代码
  • I even tried reverting to a prior commit, and for some reason, it still had the same error. 我什至尝试恢复到先前的提交,由于某种原因,它仍然有相同的错误。
  • I have deduced from my programming sessions that when the console refers to JS or JQuery on a low level line number (2 in this case), it usually has something to do with a trivial ; 我从编程会话中得出结论,当控制台在较低的行号(在本例中为2)中引用JS或JQuery时,通常与琐碎的事情有关; , ) , or } , but JSLint didn't show up that particular anomaly. ) ,或} ,但JSLint的没有出现特定的异常。
  • no browser change since then, but I did reboot, and potentially had system updates waiting to finish. 此后没有任何浏览器更改,但是我确实重新启动了,并且可能要等待系统更新完成。

Is it possible that the API of jQuery changed? jQuery的API是否有可能更改? something else that I'm not thinking of? 我没有想到的其他事情? Your suggestions as to possible culprits is appreciated. 感谢您对可能的罪魁祸首的建议。

The selector is just a string. 选择器只是一个字符串。 If your variable is already a string type, then you don't need to add quotes to each end and doing so will probably cause an error as there are not supposed to be quotes on each end of the string. 如果您的变量已经是字符串类型,那么您不需要在每个末端添加引号,并且这样做可能会导致错误,因为不应在字符串的每个末端加上引号。 So, $(var) should work just fine. 因此, $(var)应该可以正常工作。

Quotes are used to declare a string literal like: 引号用于声明字符串文字,例如:

"#test"

but are not needed if you already have the data in a variable. 但如果您已经将数据包含在变量中,则不需要。 So, if you have a variable called sel that already contains the string #test , then you can just do: 因此,如果您拥有一个已经包含字符串#test名为sel的变量,那么您可以这样做:

$(sel)

The only time you might add quotes into a selector string is if you need quotes embedded into the middle of the string such as this selector: 唯一可能将引号添加到选择器字符串中的情况是,如果需要将引号嵌入到字符串的中间,例如此选择器:

[data-myval='whatever']

I don't know you ever used $("'" + newClasses + "'") in the first place. 我不知道您是否曾经使用$("'" + newClasses + "'") Why are you adding ' before and after the string? 为什么在字符串前后添加'

That is like doing $("'.fs.btn.heading.abstract'") . 就像做$("'.fs.btn.heading.abstract'") Which I can't see why you would have ever done. 我不明白你为什么会这样做。

The only time quotes are needed inside a jQuery selector is when you are using an attribute selector like: $('input[id="test.input"]') . jQuery选择器中唯一需要加引号的时间是使用属性选择器,例如: $('input[id="test.input"]')

It is impossible you managed to make that code to work, the only possible explanation is that the code was never executed prior to adding that function. 您不可能使该代码正常工作,唯一可能的解释是该代码从未在添加该功能之前执行过。

What it is basically doing is: 它的基本作用是:

var id= "#idname"; //#idname
var str = "'" + id + "'"; //'#idname'
$(str); //equivalent to $("'#idname'"); which is wrong

Remember that when you see "hello" , the string is holding only hello , the quotes are not part of the string 请记住,当你看到"hello" ,该字符串只抱着hello ,引号不是字符串的一部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM