document.write() 在页面加载后调用时删除所有页面内容

document.write() Is Removing All Page Content When Calling After Page Load

我有一个网页,我正在使用 JSON 从他的供稿中阅读 Google Blogger 博客类别。我有两个功能。首先是获取所有博客类别列表,第二个是从中获取博客类别,然后再次点击博客以从中获取最新帖子这是文本测试,用于查看网页数据是否存在。

<div id="blogCategoriesList">
<script type="text/javascript">
var blogurl = "https://googleblog.blogspot.com/";
function blogCatList(json) {
    document.write('<select onchange="showThisCatPosts(this.value)">');
    document.write('<option>CHOOSE A CATEGORY</option>');
    for (var i = 0; i < json.feed.category.length; i++)
    {
        var item = "<option value='" + json.feed.category[i].term + "'>" + json.feed.category[i].term + "</option>";
        document.write(item);
    }
    document.write('</select>');
}                   
document.write('<script type=\"text/javascript\" src=\"' + blogurl + '/feeds/posts/default?redirect=false&orderby=published&alt=json-in-script&callback=blogCatList&max-results=500\"><\/script>');
document.write('<br/><br/><a href=\"' + blogurl + '\" target=\"_blank\" class=\"footerLINK\">Read The Blog Online Now<\/a>');
</script>
</div>
<div id="blogCategoriesPost">
<script style='text/javascript'>  
var blogurl = "https://googleblog.blogspot.com/";
var numposts = 10;  // Out Of 500
var displaymore = true;
var showcommentnum = true;
var showpostdate = true;
var showpostsummary = true;
var numchars = 100;
function blogCategoriesPost(json) {                  
    if(json.feed.entry.length < numposts ){
        numposts = json.feed.entry.length;
    }
    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 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] = "Jan";
        monthnames[2] = "Feb";
        monthnames[3] = "Mar";
        monthnames[4] = "Apr";
        monthnames[5] = "May";
        monthnames[6] = "Jun";
        monthnames[7] = "Jul";
        monthnames[8] = "Aug";
        monthnames[9] = "Sep";
        monthnames[10] = "Oct";
        monthnames[11] = "Nov";
        monthnames[12] = "Dec";
        document.write('<div id="mainDIV">');                        
        document.write('<h2 class="post_heading">' + posttitle + '</h2>');
        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, "");   // Will Show Only Text Instead Of HTML
        if (showpostsummary == true) {
            if (postcontent.length < numchars) {
                document.write('<span class="post_summary">');
                document.write(postcontent);
                document.write('</span>');
            } else {
                //document.getElementById("catPosts").innerHTML += '<span class="post_summary">';
                document.write('<span class="post_summary">');
                postcontent = postcontent.substring(0, numchars);
                var quoteEnd = postcontent.lastIndexOf(" ");
                postcontent = postcontent.substring(0, quoteEnd);
                document.write(postcontent + '...');
                document.write('</span>');
            }
        }
        var towrite = '';
        document.write('<strong class="post_footer">');
        if (showpostdate == true) {
            towrite = 'Published On: ' + towrite + monthnames[parseInt(cdmonth, 10)] + '-' + cdday + '-' + cdyear;
        }
        if (showcommentnum == true) {
            if (commenttext == '1 Comments') commenttext = '1 Comment';
            if (commenttext == '0 Comments') commenttext = 'No Comments';
            commenttext = '<br/><a href="' + commenturl + '" target ="_blank">' + commenttext + '</a>';
            towrite = towrite + commenttext;
        }
        if (displaymore == true) {
            towrite = towrite + '<br/><a href="' + posturl + '" target="_blank">Read Full Article --></a>';
        }
        document.write(towrite);
        document.write('</strong></div>');
    }                    
}
function showThisCatPosts(BLOGCAT){
    document.write('<script type=\"text/javascript\" src=\"' + blogurl + '/feeds/posts/default/-/' + BLOGCAT + '?redirect=false&orderby=published&alt=json-in-script&callback=blogCategoriesPost&max-results=500\"><\/script>');
    document.write('<a href=\"' + blogurl + '\" target=\"_blank\" class=\"footerLINK\">Read The Blog Online Now<\/a>');
}
</script>

您可以在 JSBIN 看到一个有效的 DEMO。我的问题是,当页面加载时,它可以完美地显示所有页面数据和博客类别列表,但是当我 select 任何类别时,我的所有页面数据都会删除,只有标签帖子可见。为什么会这样……???我只想根据标签更改帖子而不是删除所有页面数据...

这是 document.write() 的正常行为。您可能需要使用:

document.getElementById("element_id").innerHTML = 'Stuff to Write.';

终于解决了。原因是 document.write() 不好,因为它在页面加载时写入文本。如果你想在页面加载后写一些文本,你可以使用它。

如果你想稍后再写,那么必须使用 document.getElementById("element_id").innerHTML 来写你的文本,但是如果你想写 <script> 标签,那么 document.getElementById("element_id").innerHTML 并不好,因为它会写但是不要点击 SRC,所以使用 document.createElement("script") 在页面加载后编写脚本也可以运行。

JSBIN... 查看我的代码的工作演示...:)

特别感谢:@Praveen Kumar :-)