访问<对象>使用 JavaScript 的数据 [英] Access <object> data with JavaScript
本文介绍了访问<对象>使用 JavaScript 的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的 标签:
I have an <object>
tag like this:
<object id="text" data="file.txt"></object>
我可以得到这样的元素:
And I can get the element like this:
var text = document.getElementById("text");
现在,我如何获取文本文件的内容?
Now, how do I get the content of the text file?
这样做的目的是:
- 避免必须发出 XMLHttpRequest;
- 避免在代码中使用常量形式的文本数据;
- 以原始形式保留一个简单的纯文本文件,没有 JSON 或特殊格式,以供代码访问并成为字符串变量.
以同样的方式,您可以在文档中使用 或
标签,然后访问图像/声音,仅用于文本文件.
In the same fashion you can have an <img>
or <audio>
tag in the document and then access the image / sound, just for text files.
PS:我几乎肯定 <object>
标签不是这个的理想标签.这就是我试图完成我需要的.
PS: I'm almost positive the <object>
tag is not the ideal tag for this. This is just as far as I came trying to accomplish what I need.
推荐答案
您可以在隐藏的 iframe 中加载文本文件并访问其中的内容:
You can load your text file within a hidden iframe and access the contents within that:
<html>
<head>
<script type="text/javascript">
function gettext() {
alert(document.getElementById('ifr').contentDocument.body.innerText);
}
</script>
</head>
<body>
<iframe id="ifr" style="display:none" src="test.txt" onload="gettext()"></iframe>
</body>
</html>
这篇关于访问<对象>使用 JavaScript 的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文