$(function () {
var sr = 'section.row';
var fot = 'figure.one_third';
var pA = 'div.portfolio ul li a';
var item = ('sr fot pA');
$item.addClass('blue');
});
views:
66answers:
5
A:
I think I see two things:
Javascript variables must begin with a letter or _ (not a $)(edit: never mind)- You have extra single quotes in the $ selector, so instead of searching for
div.portfolio ul li a
, it's searching for the string" + pA + "
Tom Smilack
2010-10-28 03:39:05
That isn't true on (1)., variables that begin with `$` are very common with jQuery. `$` doesn't make a variable special either, it just denotes a jQuery object by convention.
Evan Carroll
2010-10-28 03:41:09
So it does work. But why would w3schools lie to me!?
Tom Smilack
2010-10-28 03:48:46
A:
As cambraca said in a comment, no quotes.
$(function () {
var pA = 'div.portfolio ul li a';
var $item = $(pA);
$item.addClass('blue');
});
You do write
var $item = $("div.portfolio ul li a");
But thats only because you need to have a way to mark the selector div.portfolio ul li a
as a non-javascript query. If you explicitly make a String with those contents, then quoting it would read like this.
var $item = $('"' . '"div.portfolio ul li a"' .'"');
And, that doesn't make sense.
Evan Carroll
2010-10-28 03:39:24
+4
A:
the line needs to be:
var item = $(pA);
item.addClass('blue');
or in your updated question:
var item = $(sr + ' ' + fot + ' ' + pA);
if that's what you want.
動靜能量
2010-10-28 03:41:12
+1
A:
sr and fot are not getting evaluated as variables, they are just part of the string.
You need
$(sr + "," + fot + "," + pA)
Samo
2010-10-28 03:42:55
As a side note, I think your variable names are very nondescript. Ask yourself if another developer would have any idea what those variable names mean. Or if you would have any idea what they mean 6 months down the road. Readability -> Maintainability my friend
Samo
2010-10-28 03:44:33