Javascript 从json获取数据并用作链接
data.query.results.item[0]。链接将返回一个链接。现在有没有办法将其用作标记内部的链接 HTML:Javascript 从json获取数据并用作链接,javascript,html,Javascript,Html,data.query.results.item[0]。链接将返回一个链接。现在有没有办法将其用作标记内部的链接 HTML: 您可以执行以下操作: var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*from%20feed%20where%20url%3D%22http%3A%2F%2Fwww.amaderbarisal.com%2Ffeed%22&format=json&diagnostics=true&
您可以执行以下操作:
var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*from%20feed%20where%20url%3D%22http%3A%2F%2Fwww.amaderbarisal.com%2Ffeed%22&format=json&diagnostics=true&callback?';
$.getJSON(url,function(data){
var a = $('<a>').attr('href', data.query.results.item[0].link);
$('#content').append(a);
}
这将附加一个指向具有id内容的div的链接。根据锚点是否已存在或需要创建,您可以使用:
//already exist
document.getElementById("somea").href="http://mysite.com";
document.getElementById("somea").innerHTML="text of the link";
//create one
var myA=document.createElement("a");
myA.innerHTML="text of the link";
myA.href="http://mysite.com";
document.getElementById("somediv").appendChild(myA);
我不确定您是要求使用链接触发getJSON还是使用响应构建锚定标记。如果您指的是使用jquery的后者,为什么不直接使用它来构建链接呢
$('<a>',{
text: 'Text',
title: 'Blah',
href: data.query.results.item[0].link,
click: nil }
}).appendTo('body');
注:以上代码未经测试。请提供一些更新问题的代码@Аааааааааа
var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*from%20feed%20where%20url%3D%22http%3A%2F%2Fwww.amaderbarisal.com%2Ffeed%22&format=json&diagnostics=true&callback?';
$.getJSON(url,function(data){
var a = $('<a>').attr('href', data.query.results.item[0].link);
$('#content').append(a);
}
//already exist
document.getElementById("somea").href="http://mysite.com";
document.getElementById("somea").innerHTML="text of the link";
//create one
var myA=document.createElement("a");
myA.innerHTML="text of the link";
myA.href="http://mysite.com";
document.getElementById("somediv").appendChild(myA);
$('<a>',{
text: 'Text',
title: 'Blah',
href: data.query.results.item[0].link,
click: nil }
}).appendTo('body');