我想将内容拆分为两个不同的标签。第一个变量(summarySentence1和2)无法存储结果,第二个变量(summarySentanceOthers)存储源字段的所有内容。相反,我想将内容从第一个变量中的0,28和另一个变量中的28分割到最后一个。
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>
&#13;
答案 0 :(得分:0)
请尝试以下我修改了您的代码:
<script>
$(document).ready(function () {
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>
答案 1 :(得分:0)
我认为您的问题是由于您的代码未包含在jQuery document ready function
中
$(function() {
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(' ');
$('#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>
答案 2 :(得分:0)
您需要执行以下任一操作:
var summaryText = $('body').find('.review-body .field-name-field-review-summary').text();
或简单地说,
var summaryText = $('.review-body .field-name-field-review-summary').text();
您的this
引用是指window
对象,它不了解您内容的正文。
var summaryText = $('.review-body .field-name-field-review-summary').text();
var summarySentence1and2 = summaryText.split(' ').slice(0, 28).join(' ');
var summarySentanceOthers = summaryText.split(' ').slice(28).join(' ');
console.log(summarySentence1and2);
console.log(summarySentanceOthers);
$('#first').text(summarySentence1and2);
$('#second').text(summarySentanceOthers);
.as-console-wrapper { display: none !important; }
<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>
答案 3 :(得分:0)
至于我对你提出的问题的理解。 请使用&#34; $(&#39; .review-body&#39;)。text();&#34;而不是&#34; $(this).find(&#39; .review-body .field-name-field-review-summary&#39;)。text();&#34;
var summaryText = $('.review-body').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);
&#13;
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/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>
&#13;
答案 4 :(得分:0)
添加.trim()解决问题
var summaryText = $(this).find('.review-body .field-name-field-review-summary').text().trim();