我想將內容分成兩個不同的標籤。第一個變量(summarySentence1和2)未能存儲結果,第二個變量(summarySentanceOthers)存儲源字段的所有內容。相反,我想將第一個變量中的0,28和28中的內容分割爲另一個變量中的最後一個。Jquery Slice,substr,子串不起作用
var summaryText = $(this).find('.review-body .field-name-field-review-summary').text();
var summarySentence1and2 = summaryText.split(' ').slice(0, 28).join(' ');
var summarySentanceOthers = summaryText.split(' ').slice(28).join(' ');
//alert(summarySentence1and2);
//alert(summarySentanceOthers);
$('#first').text(summarySentence1and2);
$('#second').text(summarySentanceOthers);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="review-body" itemprop="reviewBody">
<div class="field-name-field-review-summary">
We had an excellent guide in Antonino who took us on some excellent routes and had extensive knowledge of the area, fauna and history of the volcanoes and the evolution of the landscape. The hotels and included meals were disappointing and should have been of a higher quality.
</div>
</div>
<p id="first"></p>
<p id="second"></p>
我可以用你的代碼中看到的唯一的問題是它不是包裹文檔準備功能,當它似乎工作正常。 – George