无法使用jQuery appendTo向div添加新的feed

时间:2011-04-10 10:23:32

标签: jquery

我正在尝试为每个Feed项创建一个新div,并将它们添加到容器div中。当我使用$("div").append(entry.title).appendTo("#content");时 它没有显示任何东西。

以下是我的代码

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

1 个答案:

答案 0 :(得分:3)

您是否尝试创建新的<div>元素?如果是这样,您需要执行以下操作:

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

通过简单地使用$('div'),jQuery对文档中的现有元素执行查找。将标记括在<>中会创建一个新元素。