Jquery: Error: Syntax error, unrecognized expression: [name=someVar]
سؤال
Simple enough code:
$("meta[name=DCSext.Level]")
Error:
Error: Syntax error, unrecognized expression: [name=DCSext.Level]
I upgraded from 1.4.2 to 1.7.1 in Jquery. It obviously still works in 1.4.2.
Any thoughts?
المحلول
The syntax should be [name="DCSext.Level"]
as specified in the jQuery API. I recommend using single quotes for strings in JS so that you don't have to escape double-quotes, which I prefer to use for HTML:
$('meta[name="DCSext.Level"]')
Although you could continue to use double quotes:
$("meta[name=\"DCSext.Level\"]");
And you could swap the quote usage:
$("meta[name='DCSext.Level']");
This all of course assumes that the attribute itself is as follows:
<meta name="DCSext.Level" ... />
If DCSext is a JS object, you'll need to concatenate it into the string:
$('meta[name="' + DCSext.Level + '"]');
نصائح أخرى
You should add quotes if it is string:
$("meta[name='DCSext.Level']")
Or if it is variable:
$("meta[name='" + DCSext.Level + "']")
or if DCSext.Level is a variable:
$('meta[name="' + DCSext.Level + '"]');
لا تنتمي إلى StackOverflow