Javascript 如何在Blogger中通过json检索位置
我在Blogger中使用此脚本通过JSON显示带标签的帖子, 正如您通过这段代码看到的,我可以检索帖子的标题、缩略图和日期等Javascript 如何在Blogger中通过json检索位置,javascript,post,blogs,blogger,Javascript,Post,Blogs,Blogger,我在Blogger中使用此脚本通过JSON显示带标签的帖子, 正如您通过这段代码看到的,我可以检索帖子的标题、缩略图和日期等 <script type='text/javascript'> //<![CDATA[ function labelthumbs(json) { document.write('<ul id="label_with_thumbs">'); for (var i = 0; i < nump
<script type='text/javascript'>
//<![CDATA[
function labelthumbs(json) {
document.write('<ul id="label_with_thumbs">');
for (var i = 0; i < numposts; i++) {
var entry = json.feed.entry[i];
var posttitle = entry.title.$t;
var posturl;
if (i == json.feed.entry.length) break;
for (var k = 0; k < entry.link.length; k++) {
if (entry.link[k].rel == 'replies' && entry.link[k].type == 'text/html') {
var commenttext = entry.link[k].title;
var commenturl = entry.link[k].href;
}
if (entry.link[k].rel == 'alternate') {
posturl = entry.link[k].href;
break;
}
}
var thumburl;
try {
thumburl = entry.media$thumbnail.url;
} catch (error) {
s = entry.content.$t;
a = s.indexOf("<img");
b = s.indexOf("src=\"", a);
c = s.indexOf("\"", b + 5);
d = s.substr(b + 5, c - b - 5);
if ((a != -1) && (b != -1) && (c != -1) && (d != "")) {
thumburl = d;
} else thumburl = 'https://1.bp.blogspot.com/-etyXYLaEXP8/WrmiznwRzXI/AAAAAAAAAnQ/Pu4OVqzx_4kmgwnmxJGDuf6j-l6ARuHbgCLcBGAs/s1600/nothumbnailimage.png';
}
var postdate = entry.published.$t;
var cdyear = postdate.substring(0, 4);
var cdmonth = postdate.substring(5, 7);
var cdday = postdate.substring(8, 10);
var monthnames = new Array();
monthnames[1] = "January";
monthnames[2] = "February";
monthnames[3] = "March";
monthnames[4] = "April";
monthnames[5] = "May";
monthnames[6] = "June";
monthnames[7] = "July";
monthnames[8] = "August";
monthnames[9] = "September";
monthnames[10] = "October";
monthnames[11] = "November";
monthnames[12] = "December";
document.write('<li>');
if (showpostthumbnails == true)
document.write('<a class="posturl" href="' + posturl + '" target ="_top"><img class="label_thumb" src="' + thumburl + '"/></a>');
document.write('<a class="posturl" href="' + posturl + '" target ="_blank"><h3 class="posttitle">' + posttitle + '</h3><span class="dateposted">' + monthnames[parseInt(cdmonth, 10)] + ' ' + cdday + ', ' + cdyear + '></span></a>');
if ("content" in entry) {
var postcontent = entry.content.$t;
} else
if ("summary" in entry) {
var postcontent = entry.summary.$t;
} else var postcontent = "";
var re = /<\S[^>]*>/g;
postcontent = postcontent.replace(re, "");
if (showpostsummary == true) {
if (postcontent.length < numchars) {
document.write('');
document.write(postcontent);
document.write('');
} else {
document.write('');
postcontent = postcontent.substring(0, numchars);
var quoteEnd = postcontent.lastIndexOf(" ");
postcontent = postcontent.substring(0, quoteEnd);
document.write(postcontent + '...');
document.write('');
}
}
var towrite = '';
var flag = 0;
if (showpostdate == true) {
flag = 1;
}
if (showcommentnum == true) {
if (flag == 1) {
towrite = towrite + ' | ';
}
if (commenttext == '1 Comments') commenttext = '1 Comment';
if (commenttext == '0 Comments') commenttext = 'No Comments';
commenttext = '<a href="' + commenturl + '" target ="_top">' + commenttext + '</a>';
towrite = towrite + commenttext;
flag = 1;;
}
if (displaymore == true) {
if (flag == 1) flag = 1;
}
document.write(towrite);
document.write('</li>');
if (displayseparator == true)
if (i != (numposts - 1))
document.write('');
}
document.write('</ul>');
}
//]]>
</script>
// 通过post编辑器输入的位置数据可通过键georss$featurename
在JSON提要中获得。您必须在代码中包含这些内容
..
var entry = json.feed.entry[i];
var posttitle = entry.title.$t;
var location = entry.georss$featurename.$t;
var posturl;
..
你知道吗?我通常在博客上看到你的创造性回答,今天我希望你能回答我,而现在你做到了!它起作用了!因此,通过json可以检索post中具有特定类/id的任何其他元素,不是吗?我知道我可以使用(entry.content.$t)检索整个内容。。但是,如果我想检索,例如Hello World
,您必须首先通过entry.content.$t
检索文章的完整HTML内容,然后解析它以从中提取特定元素-请参阅,不幸的是,我没有找到任何准确的答案来解释如何检索id为的元素,from entry.content.$t:/您很聪明,我相信您可以简化为我,谢谢
..
var entry = json.feed.entry[i];
var posttitle = entry.title.$t;
var location = entry.georss$featurename.$t;
var posturl;
..
document.write('Location: ' + location + '<br/><a class="posturl" href="' + posturl + '" target ="_blank"><h3 class="posttitle">' + posttitle + '</h3><span class="dateposted">' + monthnames[parseInt(cdmonth, 10)] + ' ' + cdday + ', ' + cdyear + '></span></a>');