divを見て、最初の単語を叩くjQueryがあります。これはうまく動作しますが、そのdivには1つの単語しかない場合、私はそれを打ち切りたいとは思いません。jQuery - divに2つの単語がある場合、最初の単語を打ち破る
誰でもこれを達成する方法を知っていますか?
コード:
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);
});
good ol'ififはどうですか? –
'$(this).text()。split( '').length> 1 'の長さをチェックしてから、何かを打ち切ります – Developer