开发者

How do I pass a variable into a jQuery query?

For some reason, this works:

var oldText = $("#tayke li:eq(" + theIndex + 开发者_运维技巧")" ).text();

But this doesn't:

var tayke_li =  "#tayke li:eq(" + theIndex + ")"
var oldTest = $( tayke_li ).text();

Note: theIndex is an integer.


It works both ways. I've redone it and it worked. Check the theIndex variable for changes and scope. Try replacing it with hardcoded 1

adding jQuery version info to the question, and a browser spec would be nice too.


Does it make any difference if you put the semi-colon at the end of then line:

var tayke_li =  "#tayke li:eq(" + theIndex + ")"; //<---

I've set up a simple example, and it works fine:

alert($("#tab1").length);
var s = "#tab" + String(1); //alerts "1"
alert(s);  //alerts "#tab1"
alert($(s).length); //alerts 1

Also, try an explicit cast of theIndex to a string using String(). Have you double-checked what is stored in tayke_li

Semicolons Required?

Do you recommend using semicolons after every statement in JavaScript?

What is the consequence of this bit of javascript?

Should I use semicolons in JavaScript?

http://www.webmasterworld.com/forum91/521.htm


try

var oldText = $("#tayke li:eq("+parseInt(theIndex)+")").text();

or

var oldText = $("#tayke li").eq(theIndex).text();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜