第n个孩子的变数

时间:2014-08-15 02:55:57

标签: javascript jquery css variables

您好我在我的第n个孩子Jquery代码中使用变量时遇到了问题。我已多次尝试过它并没有奏效。数字1有效但变量和位置没有。我尝试过各种形式的('+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();
});

2 个答案:

答案 0 :(得分:3)

正如评论中提到的@ 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();
});

答案 1 :(得分:0)

考虑更改您的代码

$('#tHacks tr:nth-child('+position+')').hide();

$('#tHacks tr:nth-child(4)').hide();

您可以在浏览器的控制台窗口中运行此代码,看它是否有效。

可能会发生整数正在添加到字符串中,从而在代码中产生问题。或者可能发生在您的代码中引用的第n个孩子不存在。

相关问题