我希望能够隐藏少于 3 个字符的列表项,我该怎么做?我下面的代码有什么问题?
我是 JavaScript/jQuery 新手。
jQuery().ready(function () {
if (jQuery('ol li').length < 3) {
jQuery(this).hide();
};
});
最佳答案
你的代码在说
if (jQuery('ol li').length < 3) { //If I have less than 3 li elements
jQuery(this).hide(); //hide the window object
};
你要用的是过滤器
$('ol li').filter( function(){ return $(this).text().length<3; } ).hide();
编辑 - 根据您在我的帖子中的评论:如果它是一个 span 标签,周围可能有其他数据:
$('ol li span').filter( function(){ return $(this).text().length<3; } ).parent().hide()
关于javascript - 如果 li 少于 3 个字符,隐藏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11977388/