2011-04-10 19 views
0

我正在嘗試爲每個Feed項目創建一個新div,並將它們附加到容器div。當我使用$("div").append(entry.title).appendTo("#content"); 它沒有顯示任何東西。無法使用jQuery將新feed添加到div appendTo

下面是我的代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
    <title>Google AJAX Search API Sample</title> 
    <script src="http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0" type="text/javascript"></script> 
    <script type="text/javascript" src="javascript/jquery-1.4.2.js"></script> 
    <script type="text/javascript"> 
    /* 
    * How to load a feed via the Feeds API. 
    */ 
    $(document).ready(function(){ 

    var feed = new google.feeds.Feed("http://www.digg.com/rss/index.xml"); 

    // Calling load sends the request off. It requires a callback function. 
    feed.load(feedLoaded); 
    }); 


    google.load("feeds", "1"); 
    // Our callback function, for when a feed is loaded. 
    function feedLoaded(result) { 
     if (!result.error) { 
     // Grab the container we will put the results into 
     var container = document.getElementById("content"); 
     container.innerHTML = ''; 

     // Loop through the feeds, putting the titles onto the page. 
     // Check out the result object for a list of properties returned in each entry. 
     // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON 
     for (var i = 0; i < result.feed.entries.length; i++) { 
      var entry = result.feed.entries[i]; 
     // var div = document.cre ateElement("div"); 
     //   div.appendChild(document.createTextNode(entry.title)); 
     // container.appendChild(div); 

     $("div").append(entry.title).appendTo("#content"); 

     } 
     } 
    } 


    // 

    </script> 
    </head> 
    <body style="font-family: Arial;border: 0 none;"> 
    <div id="content">Loading...</div> 
    </body> 
</html> 

回答

3

你們是不是要創建一個新的<div>元素?如果是這樣,你需要做到以下幾點:

$('<div>').append(entry.title).appendTo('#content'); 

通過簡單地使用$(「格」),jQuery的執行對文檔中現有元素查找。將標籤包含在<>中將創建一個新元素。

+0

非常感謝。它爲我工作。 – JavaGeek 2011-04-10 10:32:50