我为什么不能像这样将num变量传递给此函数:
function getColumnData(num) {
var colVals = $('#newtable td:nth-child(num)').map(function(){
return $(this).text();
}).get();
alert(colVals);
}
有没有解决此问题的简单方法?
谢谢。
最佳答案
您需要将num
的值与选择器字符串连接起来:
var colVals = $('#newtable td:nth-child(' + num + ')').map(function() {
//Do stuff
});
当前,您只有一个字符串文字“ #newtable td:nth-child(num)”,而不是对选择器引擎有意义的东西,例如“ #newtable td:nth-child(2)”。
关于jquery - 在jQuery中传递数值变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10685081/