开发者

what is the difference between eq("+index+") and eq(index) in jQuery

开发者 https://www.devze.com 2023-04-07 14:18 出处:网络
开发者_如何学GoIn an example where eq() function is used, it was used aseq(\"+index+\") I haven\'t seen syntax like this before. What does \"+\" sign on both sides mean? How is it different from eq(i

开发者_如何学GoIn an example where eq() function is used, it was used as eq("+index+")

I haven't seen syntax like this before. What does "+" sign on both sides mean? How is it different from eq(index)?

Thank you!


In jQuery, eq refers to two slightly different things: .eq, the function, and :eq, the selector.

The function version is chained onto a jQuery object, so you'd see examples like:

$(".whatever").eq(index)

Whereas the other form is used as part of the selector string, so you'll see people concatenate the index with the rest of the string:

$(".whatever:eq(" + index + ")")

For performance reasons (and better readability in many cases), the jQuery documentation recommends the first form, the .eq function.


if you are using eq as a function you should use it like

$("div").eq(3).html("test");

if as a selector:

var index = 3;
$("div:eq(" + index + ")").html("test");

2 ways, same result....


Probably you were looking at an example similar to this:

$("foo:eq(" + index + ")")

You are probably comparing that to something like this:

$("foo").eq(index)

The difference is that the former is a (faux) CSS selector syntax and the latter is just a method call. Compare http://api.jquery.com/eq-selector/ to http://api.jquery.com/eq/.

0

精彩评论

暂无评论...
验证码 换一张
取 消