(function (){
var stones = parseInt($('body').attr('data-site')) + 1,
theul = $(".submenu > ul li:nth-child(" + stones + ")");
console.log(theul);
$('theul').addClass('active');
console.log(theul);
})();
日志在添加类之前和之后显示完全相同的内容。
您的选择器错误,应该是:
theul.addClass('active');
简单地做
var stones = parseInt($('body').attr('data-site')) + 1,
theul = $(".submenu > ul li:nth-child(" + stones + ")");
console.log(theul);
theul.addClass('active');
console.log(theul);
})();
因为 $('theul').addClass('active');
会将类添加active
到 ipotetical 标签的元素中theul
它应该是 theul.addClass('active');