基于以下模式,如何创建一个变量来从json中获取标签?
var postLink = function(entry) {
for (var j = 0; j < entry.link.length; j++) {
if (entry.link[j].rel == 'alternate') {
var postURL = entry.link[j].href;
return postURL;
break;
}
}
};
var postTitle = function(entry) {
var postTitle = entry.title.$t;
return postTitle;
};
function Full(json) {
for (var i = 0; i < json.feed.entry.length; i++) {
var entry = json.feed.entry[i];
var item1 = '<h3><a href="' + postLink(entry) + '">' + postTitle(entry) + '</a></h3>';
document.write(item1);
}
}
此代码打印结果:
<script src="/feeds/posts/summary/-/LABEL?alt=json-in-script&callback=Full&max-results=2"></script>
结果仅用于帖子标题和链接...我只是想为标签添加变量代码...
我想要内部代码:
var postLabel = function(entry) {
//some code here
};
...在var item1
内,我会添加:+ postLabel(entry) +
我需要遵循postLink和postTitle的模式,但这超出了我的能力......感谢您的帮助!
答案 0 :(得分:0)
经过数小时的反复试验,我找到了一个基于其他类似代码的解决方案!我只知道CSS和我开始喜欢js :))
var postLabel = function(entry) {
var postLabel = entry.category[0].term;
return postLabel;
};