【问题标题】:Variables in JQuery selector aren't workingJQuery 选择器中的变量不起作用
【发布时间】:2013-01-29 12:48:24
【问题描述】:
我有一个函数,它采用 x 和 y 值,并使用表格的行和列,找到给定的单元格。 jQuery:
function changeNum(x,y,num)
{
var thisRow = $(".sudoku:nth-child("+y+")");
var thisCell = $(thisRow+":nth-child("+x+")");
}
thisCell 声明中的某些内容导致 javascript 停止。
【问题讨论】:
标签:
javascript
jquery
variables
jquery-selectors
【解决方案1】:
thisRow 是一个 jQuery 集合,而不是一个字符串。使用这个:
var thisCell = $(":nth-child("+x+")", thisRow);
你也可以直接使用
var thisCell = $(".sudoku:nth-child("+y+") :nth-child("+x+")");
请注意,如果sudoku 是表的类而不是行的类,那么您需要在.sudoku 和:nth-child 之间留一个空格。
【解决方案2】:
尝试用 ""+ 引导
var thisCell = $(""+thisRow+":nth-child("+x+")");