2012-12-19 42 views
0

HTML标记:取JSONP数据

<ul> 
    <li class="entries"></li> 
</ul> 

jQuery代码:

$(function() { 
    $.ajax({ 
     url: "http://query.yahooapis.com/v1/public/yql", 
     dataType: "jsonp", 
     success: function (data) { 
      console.log(data.query.results.json); 
      $.each(data.query.results.json.entries, function (i, v) { 
       $('#entries').append(data.query.results.json.entries[i].content + '<br />'); 
      }); 
     }, data: { 
      q: 'select * from json where url="https://www.facebook.com/feeds/page.php?id=397319800348866&format=json"', 
      format: "json" 
     } 
    }); 
}); 

FIDDLE

我想看到的输出上的标题menu.when点击它会显示内容和形象也。我将如何使用jQuery和HTML做到这一点?

+0

问题应该是“如何格式rss饲料?”和ans更可能是在谷歌 – efkah

+0

你在开玩笑吗?这不feeds.this被格式化使用jsonp .... – sami

回答

2

创建每个条目的标题和内容元素,当你点击标题切换内容元素的可见性:

的Javascript:

$(function() { 
    $.ajax({ 
     url: "http://query.yahooapis.com/v1/public/yql", 
     dataType: "jsonp", 
     success: function (data) { 
      console.log(data.query.results.json); 
      $.each(data.query.results.json.entries, function (i, v) { 
       $('#entries').append(
        $('<div>').addClass('header') 
        .html(data.query.results.json.entries[i].title) 
        .append(
         $('<div>').addClass('content') 
         .html(data.query.results.json.entries[i].content) 
        ) 
       ); 
      }); 
     }, data: { 
      q: 'select * from json where url="https://www.facebook.com/feeds/page.php?id=397319800348866&format=json"', 
      format: "json" 
     } 
    }); 

    $('#entries').on('click', '.header', function(){ 
     $('.content', this).toggle(); 
    }); 
}); 

CSS:

.header { margin: 5px; background: #eee; padding: 5px; } 
.content { display: none; } 

演示:http://jsfiddle.net/DtNxb/5/

+0

当我打开你的jsfiddle我不能使它的工作,它甚至不会做http调用。与OP的jsfiddle一样。可能是什么问题呢? – vtortola

+0

@vtortola:我不确定,我已经在Firefox,IE和Chrome上测试过了,它在这里有效。检查Firebug或IE Developer Tools等工具中的网络流量,查看请求返回的内容。 – Guffa

+0

在Chrome和IE9中无法使用。实际上,它甚至不启动HTTP请求,我在检查员中看不到任何对yahoo域的请求。这很奇怪。 – vtortola