gpt4 book ai didi

javascript - 调用文本文件以填充 HTML 中的段落 - 这可能吗?

转载 作者:搜寻专家 更新时间:2023-10-31 08:54:39 25 4
gpt4 key购买 nike

是否可以像在图像标签中引用图像那样调用文本文件?类似于

<img src="http://link.to/image.png">

但是

<p src="http://link.to/text.txt"></p>

?

你能用 javascript 做到这一点吗?任何建议将不胜感激!

最佳答案

您可以使用 XHR (XMLHttpRequest) 来做到这一点。纯 Javascript:

<p id="text"></p>

<script>
text = document.getElementById('text');

xhr = new XMLHttpRequest();
xhr.open("GET", "http://link.to/file.txt");
xhr.send();

xhr.onreadystatechange = function() {
if (xhr.readyState == 4) //the readyState if the status of the request
text.innerHTML = xhr.responseText; // (http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp)
// 4 is a completed request
}
</script>

或者,jQuery 有 .load() 函数:

<p id="text"></p>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
$('#text').load('http://link.to/file.txt');
</script>

关于javascript - 调用文本文件以填充 HTML 中的段落 - 这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29614650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com