2014-01-30 68 views
0

我試圖以編程方式將文章添加到我的網站,然後將隨機單詞添加到使用jquery的每篇文章。我能夠製作所有文章,但我無法向文章添加文字。這裏是我的代碼:JQuery以編程方式添加文章

<!DOCTYPE html> 

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script src="http://code.jquery.com/jquery-2.1.0.min.js" type="text/javascript"></script> 
    <script src="Script/jquery.lorem.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() 
     { 
      var colors = ["#CCCCCC", "#333333", "#990099"]; 
      var rand = Math.floor(Math.random() * colors.length); 

      $(function() { 
       for (var i = 0; i < Math.floor((Math.random() * 64) + 34) ; i++) { 
        $("#Inferface").append("<article class='box' style='background-color:" + colors[rand] + "'><span>" + i + "</span>"); 
       } 
      }); 
      // This line below should be adding the random word to each of the articles 

      $('.box').lorem({ type: 'words', amount: '1', ptags: false }); 
     }); 

    </script> 
</head> 
<body id="Inferface"> 
</body> 
</html> 
+1

有你檢查了jQuery lorem插件是否正確加載到您的網頁? – Carl

+0

是的,它的工作原理。可以說我有一個

它會在正文中顯示一個單詞。 – user977154

+0

用你的代碼看這個小提琴。工作得很好。 小提琴 - http://jsfiddle.net/dVVHs/1/ – Jeff

回答

1

奇怪,在我看來,該代碼正在與極少數的調整

$(function() 
{ 
     var colors = ["#CCCCCC", "#333333", "#990099"]; 
     var rand = Math.floor(Math.random() * colors.length); 

     for (var i = 0; i < Math.floor((Math.random() * 64) + 34) ; i++) 
     { 
      $("#Interface").append("<article class='box' style='background-color:" + colors[rand] + "'><span>" + i + "</span>"); 
     } 
     // This line below should be adding the random word to each of the articles 
     $('.box').lorem({ type: 'words', amount: '1', ptags: false}); 
}); 

..你可以點擊此處查看: Working Example

+0

因此,看起來您添加的div有所不同?你知道爲什麼會這樣嗎?另外,你知道爲什麼每篇文章都不是一個不同的顏色? – user977154

+0

這可能是由於一些環境中的lorem插件的一些限制..很難告訴 –

+0

哦好吧,謝謝你的幫助。我真的很感激它:) – user977154