我有 2 个不同类别的 div。每个按钮有一排 5 个按钮,每个按钮的大小相同。 如果我单击顶行的按钮,我希望隐藏其正下方的按钮。
$('.top-row .button').click(function(){
var indexx = $(this).index()+1;
$(".bottom-row .button:nth-child(indexx)").show().fadeOut(800);
不幸的是,当我单击按钮时没有任何反应。
有什么建议吗?
最佳答案
您必须自己构建选择器字符串:
$(".bottom-row .button:nth-child(" + indexx + ")").show().fadeOut(800);
JavaScript 不会查看字符串常量来寻找要插入的变量。
关于javascript - 通过变量选择第 n 个子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27585593/