当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Display text from text file formated as html?

问题描述:

How can I display the text from content.txt and the tags in it to be displayed as regular html?

网友答案:

First, you create the file content.txt and you write in it your html source you want to be displayed. Example:
content.txt

Yep, this is the content.txt!
Some html?
<code>This is the code tag</code>
<button class="btn btn-primary" onclick="hideAjax()">Hide content.txt</button>
<h1>It works!</h1>

HTML

<div id="ajaxResponse">
    content.txt isn't displayed
</div>
    <button type="button" onclick="loadAjax()" class="btn btn-primary">Show text from content.txt</button>

JS/Ajax

 function loadAjax() {
   var xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
     if (this.readyState == 4 && this.status == 200) {
       document.getElementById("ajaxResponse").innerHTML =
       this.responseText;
     }
   };
   xhttp.open("GET", "content.txt", true);
   xhttp.send();
 }

Note: I've found this in testingc.ga, so be sure to check out their version.

分享给朋友:
您可能感兴趣的文章:
随机阅读: