動作する小さな jQuery スニペットがありますが、JS を改善するために、その効率を改善する方法について提案をお願いします。
この関数は、WordPress の抜粋を短縮します。元の WordPress Excerpt から最初の 40 単語を抽出します。これが私が気にかけている部分です。その後、元の抜粋の最後に「続きを読む」リンクを追加し、切り捨てられたバージョンの最後に追加します。
繰り返しになりますが、切り捨てられた抜粋を返すためのより高速で簡潔な方法は何でしょうか。「スライス」を試してみたところ、たくさんのコンマがありました。
jQuery('.page-id-7 .promo_slider_excerpt').each(function (i) {
var s = jQuery(this).html();
var sw = s.split(' '); // separate contents into array of words
var t = [];
var v;
if (sw.length > 40) {
var a = jQuery(this).children('a'); // this is the Continue Reading link
for (v = 0; v < 40; v++) {
t = (t + ' ' + sw[v]); // Build the shortened excerpt (this is the part I wanted to improve)
}
t = (t + ' ' + a[0].outerHTML); // Add the Continue Reading onto the end
jQuery(this).html(t);
} else {
t = s;
jQuery(this).html(t);
}
});