您好,我在第 n 个子 Jquery 代码中使用变量时遇到问题。我已经尝试了很多次,但没有奏效。数字 1 有效,但 var ab ac 和 position 无效。我尝试了各种形式的 ('+position+')
, (' + position + ')
, ('+(position)+')
。但是,没有一个有效...
var nextB = $('.nextButton');
$(nextB).click(function(){
var AA = 1;
var AB = 2;
var AC = 3;
var position = 4;
$('#tHacks tr:nth-child(1)').hide();
$('#tHacks tr:nth-child(AB)').hide();
$('#tHacks tr:nth-child(AC)').hide();
$('#tHacks tr:nth-child('+position+')').hide();
});
最佳答案
正如@j08691 在评论中提到的,您应该检查控制台是否有错误。 AB 行可能会失败并停止执行该功能。看起来位置线是正确的,所以AB和AC也是一样的。
更新: @monkeyinsight 注意到一个不相关的问题...在我的示例代码中清理它。此外,@Kay 指出您可能想使用 AA 而不是原来的 1,因此我也将其添加到解决方案中。
$('.nextButton').click(function(){
var AA = 1;
var AB = 2;
var AC = 3;
var position = 4;
$('#tHacks tr:nth-child('+AA+')').hide();
$('#tHacks tr:nth-child('+AB+')').hide();
$('#tHacks tr:nth-child('+AC+')').hide();
$('#tHacks tr:nth-child('+position+')').hide();
});
关于javascript - 第 n 个 child 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25320624/