我有一塊jQuery,它看着一個div,並擊穿第一個單詞。這工作正常,但如果該div只有一個詞,我不希望它通過。jQuery的 - 如果div有兩個單詞,通過第一個字
任何人都知道如何做到這一點?
代碼:
jQuery('.class').each(function(index) {
//get the first word
var firstWord = $(this).text().split(' ')[0];
//wrap it with strike
var replaceWord = "<strike>" + firstWord + "</strike>";
//create new string with span included
var newString = $(this).html().replace(firstWord, replaceWord);
//apply to the divs
jQuery(this).html(newString);
});
好的ol''if'怎麼樣? –
檢查'$(this).text()。split('').length> 1'的長度,然後去除東西 – Developer