从 yql 结果中检索 url
我一直在尝试了解如何从 yql 结果返回链接,但到目前为止我得到的唯一东西是 [object Object] 并且到目前为止我没有得到任何结果,你能向我解释一下为什么会发生这种情况吗?我想要的只是将结果显示为链接。
<div id="funfacts">
</div>
<script>
function funfacts(o){
var facts = document.getElementById('funfacts');
if(facts){
var data = o.query.results.a;
var out = document.createElement('a');
out.className = 'fact';
facts.appendChild(out);
out.innerHTML = data.join("<br />");
}
}
</script>
<script src="http://query.yahooapis.com/v1/public/yql?q=select%20href%20from%20html%20where%20url%3D'http%3A%2F%2Fcid-a59bebc211b678a6.office.live.com%2Fbrowse.aspx%2FPublic%2Fhi5'%20and%20xpath%3D'%2F%2Fa%5B%40id%3D%22download%22%5D'%0D%0A&format=json&diagnostics=true&callback=funfacts"></script>
我希望这个例子足以理解,我想做的就是让它显示该页面的 url 列表,但是它只返回 [object Object] 行的列表。
I have been trying to understand how to return links from yql results, but so far the only thing im getting is [object Object] and i am not getting anywhere so far, could you care to explain to me why is this happening? All I want is to display the results as links.
<div id="funfacts">
</div>
<script>
function funfacts(o){
var facts = document.getElementById('funfacts');
if(facts){
var data = o.query.results.a;
var out = document.createElement('a');
out.className = 'fact';
facts.appendChild(out);
out.innerHTML = data.join("<br />");
}
}
</script>
<script src="http://query.yahooapis.com/v1/public/yql?q=select%20href%20from%20html%20where%20url%3D'http%3A%2F%2Fcid-a59bebc211b678a6.office.live.com%2Fbrowse.aspx%2FPublic%2Fhi5'%20and%20xpath%3D'%2F%2Fa%5B%40id%3D%22download%22%5D'%0D%0A&format=json&diagnostics=true&callback=funfacts"></script>
I hope this example is enough for understanding, all i am trying to do is for it to display the list of urls from that page, however it only returns a list of [object Object] lines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您对返回对象执行 console.log,您将看到结果实际上是一个包含三个数组的数组。因此,要获取网址,您实际上需要执行类似 o.query.results.a[n].href 的
操作,这是 jsfiddle
If you do a console.log on the return object you'll see that results is actually an array with three arrays within it. Thus to get the url you would actually need to do something like o.query.results.a[n].href
Here is the jsfiddle