2017-05-03 23 views
-1

我想將內容分成兩個不同的標籤。第一個變量(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>

+0

我可以用你的代碼中看到的唯一的問題是它不是包裹文檔準備功能,當它似乎工作正常。 – George

回答

0

加入.trim()解決了這個問題

var summaryText = $(this).find('.review-body .field-name-field-review-summary').text().trim();

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> 
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>

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>

+0

它在這裏工作,但不在我的代碼中 – Nikhil

0

至於我對你的問題我沒有理解。 請使用「$('。review-body')。text();」而不是「$(this).find('。review-body .field-name-field-review-summary')。text();」

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);
<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>