2016-03-01 74 views
-1

一個網站不會在每個帖子中的單詞數量相同。有可能我們可以根據JavaScript或jQuery的單詞或字符數來對它們進行排名嗎?通過長文本與腳本排序帖子

例如創建文本長度

0-200 words 
201- 400 words 
401-600 words 

等環節

更新:

我使用的是託管的博客。

那麼,我會澄清我的問題。

我希望我可以製作一個鏈接,通過大量的文本導致特定的帖子。

例如: 我有一個頁面page1.html (has 150 words)

我希望我可以做,導致了職位和按類別排序的鏈接。

此分類基於詞頻。如果訪問者點擊鏈接類別0-200 words,它將顯示小於200 words的帖子數,包括page1.html

我希望這能解釋我的問題。謝謝

+0

您想對網站的每個頁面/帖子給出評分/排名嗎? –

+0

不,我想創建一個指向基於文本長度的特定帖子的鏈接。 例如'0 - 200 words' – Norax

+1

分享您已經嘗試過的一些代碼。至少分享包含帖子數據的對象。 – gurvinder372

回答

1

很難給出一個完整的答案,因爲我們看不到的地方,你會得到你的字數從網頁,但...

 <body> 

      <p>abc def</p> 
      <p>def ghi</p> 

     </body> 

真的,你很可能會使用類似php循環訪問一個頁面列表,最後你會得到三個數組,每個數組包含一個0-200,201-400和400+以上的頁面列表。

然後,您可以包含每個鏈接的鏈接列表。

 <script> 
      function wordCount(){ 

        var e = document.getElementsByTagName('p'); 

        var totalWords = 0; 
        for (var i = 0; i < e.length; i++) { 

         var innerTx = e[i].innerHTML; 
         var wordArray = innerTx.split(' '); 
         var thisTotal = wordArray.length; 
         // alert(wordArray.length); 
         totalWords += thisTotal; 
         // totalWords += wordArray.length; 
        //Do something 
        }//f 

        // var a = 'abc def ghi'; 
        // document.write(b.length); 


        // alert(totalWords); 
        if (totalWords > 400) { 
         alert('400+'); 
        } else if(totalWords > 200){ 
         alert('200+'); 
        } else { 
         alert('< 200'); 
        } 

        alert('Now you need to use this information to add the specific page to an array of pages with the different word counts'); 
      } 

      wordCount(); 
     </script> 
+0

好的,謝謝。我瞭解你的目的,我似乎找到了一個解決方案 – Norax