如何使用 js 将 json 响应插入 <a>xxxxx</a>

How do you use js to insert json response into <a>xxxx</a>

本文关键字:xxxxx 插入 js 何使用 json 响应      更新时间:2023-09-26

我正在尝试在我的 tumblr 帖子中实现一个"最新"的小部件。到目前为止,我已经能够从这篇文章(http://stackoverflow.com/questions/5617378/how-to-display-a-link-to-my-latest-tumblr-post-using-javascript)中使用 json 获取前 5 个最新帖子。我想知道如何将{title}插入 xxx 中,以便链接显示最近帖子的标题。

我想我需要使用 .append 或 .html,但我不确定该怎么做。 有人可以帮我吗?

贾夫文:

$.getJSON('http://brianjsmith.tumblr.com/api/read/json?callback=?',
function(response) {
   $('#myLink').attr('href',response.posts[0].title);
});

.html:

<a id=#myLink>Most recent post {title}</a>
$('#myLink').attr('href',response.posts[0].url).text(response.posts[0].title);

更新:检查响应后,没有title属性,只有regular-title

$('#myLink').attr('href',response.posts[0].url).text(response.posts[0]['regular-title']);
$("#myLink").html('Most recent post' + response.posts[0].title)