3
content.txtのテキストとそのタグを通常のhtmlとして表示するにはどうすればよいですか?テキストファイルのテキストをHTML形式で表示しますか?
content.txtのテキストとそのタグを通常のhtmlとして表示するにはどうすればよいですか?テキストファイルのテキストをHTML形式で表示しますか?
最初にcontent.txtというファイルを作成し、表示するHTMLソースを記述します。例:
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();
}
注:私はきたフンこれはtestingc.gaにありますので、バージョンを確認してください。
** content.txt **のxhttp.openではhttps://domain.tld/directory/file.txtのような完全なパスを挿入できますが、クロスオリジン(CORSエラー)には注意してください。 !! –
ファイルの内容を 'XMLHttpRequest'で取得し、' .innerHTML'を使ってコンテナの中に挿入します。 –
この質問は、質問と回答のスタイルです。私はalredyに答えました –