这是pen我正在尝试。 如果您将看到笔,则第一个容器有一个未显示在结果中的子 div。Jquery 如下,
$('.content-canvas').find('div').hide();
$('.content-canvas div:first-child').show();
$('.tab-button span:first-child').addClass('active');
$('.tab-button').find('span').click(function(){
$('.tab-button').find('span').removeClass('active');
var currentclass=$(this).attr('class');
$(this).addClass('active');
$('.content-canvas').find('div').each(function(){
if($(this).attr('class')==currentclass)
{
$('.content-canvas').find('div').hide();
$(this).slideDown(200);
$(this).children().show(200);
}
else
{
$(this).hide();
}
});
});
最佳答案
第一行:
$('.content-canvas').find('div').hide();
更改为
$('.content-canvas > div').hide();
对您使用的所有相同选择器执行相同的操作。您只需要隐藏直接后代
,而不是所有div
。
此外,我建议将此 selecotr 缓存到变量中:
var elements = $('.content-canvas > div').hide();
...
...
element.each(function() {
...
等等,这样就不用每次都跳进DOM了。
关于javascript - 选项卡的子元素不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18735475/