我希望能夠隱藏少於3個字符的列表項,我該怎麼做?我的代碼在下面有什麼問題?如果li少於3個字符,隱藏?
我是一個JavaScript/jQuery新手。
jQuery().ready(function() {
if (jQuery('ol li').length < 3) {
jQuery(this).hide();
};
});
我希望能夠隱藏少於3個字符的列表項,我該怎麼做?我的代碼在下面有什麼問題?如果li少於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();
編輯 - 根據我在文章發表的留言:如果它是一個跨度標籤,可能有其他數據周圍:
$('ol li span').filter(function(){ return $(this).text().length<3; }).parent().hide()
我想你需要讓你的DOM元素的.html()
,然後做對即.length
。
您將需要過濾掉小於3個字符的內容要素,以及隱藏這些:
$(function() {
$('ol li').filter(function() {
return $(this).text().length < 3 ;
}).hide();
});
該過濾器絕對看起來是最好的方式來做到這一點。 – 2012-08-15 21:16:36
$('ul li').each(function() {
if ($(this).text().length < 3)
$(this).hide();
});
試試這個:
$('ol li').filter(function(){ return $(this).text().length < 3; }).hide();
另一種選擇可能是這樣的:
(因爲你正在評估要素沒有價值的字符在你的問題的代碼片段)
if($('ol li').length < 3){ $(this).hide(); };
非常感謝! – 2012-08-15 21:29:07
這工作,但我想佔span標籤以及內字符若?但最終,我想隱藏整個李?謝謝! – 2012-08-15 21:21:03
比使用文字() – epascarello 2012-08-15 21:21:20
非常感謝你! – 2012-08-15 21:28:27