Discussion:
tooone7 Can't understand this... Want to make a image appear on active link
Jonathan Vanherpe (T & T nv)
2010-02-22 12:42:14 UTC
Permalink
$("ul#nav_prim a")[i]...
you need to use

$("ul#nav_prim a").eq(i)...
or
$("ul#nav_prim a:eq("+i+")")...

Jonathan
--
Jonathan Vanherpe - Tallieu & Tallieu nv - ***@tnt.be
Jonathan Vanherpe (T & T nv)
2010-02-22 13:13:32 UTC
Permalink
$('selector')[index] returns the actual DOM element, while
$('selector').eq(i) returns a jquery object (you can see a jquery object
as some sort of a shell around the actual DOM node that the jquery
functions work on).

You can only use the jquery .css() function on jquery objects, and not
directly on DOM nodes. I think it's easiest to explain by using the
console in firebug:
type $("ul#nav_prim a")[1] and $("ul#nav_prim a").eq(i) into the console
and you'll see that firebug returns a slightly different result.

As for ($("ul#nav_prim a")[i]==document.URL), I don't think this is the
proper way to do it, but if it works, great (just don't assume this will
work in all browsers, test it first).

Jonathan
Post by Jonathan Vanherpe (T & T nv)
$("ul#nav_prim a")[i]...
you need to use
$("ul#nav_prim a").eq(i)...
or
$("ul#nav_prim a:eq("+i+")")...
Jonathan
--
I changed my code to the way you suggested, the error message is now gone
but the background image doesn't change.
function displayBgImg() {
for (var i=0; i<$("ul#nav_prim a").length; i++) {
if ($("ul#nav_prim a")[i] == document.URL) {
$("ul#nav_prim a").eq(i).css("background-image",
"url(images/nav_bg.gif)");
}
}
}
it works!
Notice that i only have "eq(i)" on line 4 but not on line 3.
Do you mind explaining what's happening when i use ".eq(i)" instead of
"[i]"?
Thank you very much for the help Jonathan :)
--
Jonathan Vanherpe - Tallieu & Tallieu nv - ***@tnt.be
tooone777
2010-02-22 13:20:51 UTC
Permalink
Ohh ok cool! Thank you Jonathan. I understand it better now :)
--
View this message in context: http://old.nabble.com/tooone7-Can%27t-understand-this...-Want-to-make-a-image-appear-on-active-link-tp27686495s27240p27687083.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.
tooone777
2010-02-22 12:55:57 UTC
Permalink
Post by Jonathan Vanherpe (T & T nv)
$("ul#nav_prim a")[i]...
you need to use
$("ul#nav_prim a").eq(i)...
or
$("ul#nav_prim a:eq("+i+")")...
Jonathan
--
I changed my code to the way you suggested, the error message is now gone
but the background image doesn't change.

However, if it looks like this:

function displayBgImg() {
for (var i=0; i<$("ul#nav_prim a").length; i++) {
if ($("ul#nav_prim a")[i] == document.URL) {
$("ul#nav_prim a").eq(i).css("background-image",
"url(images/nav_bg.gif)");
}
}
}

it works!

Notice that i only have "eq(i)" on line 4 but not on line 3.

Do you mind explaining what's happening when i use ".eq(i)" instead of
"[i]"?

Thank you very much for the help Jonathan :)
--
View this message in context: http://old.nabble.com/tooone7-Can%27t-understand-this...-Want-to-make-a-image-appear-on-active-link-tp27686495s27240p27686800.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.
Loading...