读取HTML对象标签中的数据 [英] Read data in HTML object tag
问题描述
我有一个存储在服务器上的文本文件和HTML中的对象,如下所示:
I have a text file stored on the server and an object in HTML like this:
<object id="data" type="text/plain" data="test.txt"></object>
如何阅读 test.txt $ c $的内容c>在Javascript中?到目前为止我所拥有的是:
How can I read the contents of test.txt
in Javascript? What I have so far is:
var data = document.getElementByID("data");
但我无法弄清楚如何读取对象标签内的HTML文档。
But I can't figure out how to read the HTML document inside the object tag.
推荐答案
对象
标记必须向服务器发出单独请求然后加载那个内容。同时,您的JavaScript已经执行并错过了总线。
The object
tag has to make a separate request to the server and then load that content. Meanwhile, your JavaScript has already executed and "misses the bus."
在 onload
事件中运行您的代码对象
。
Run your code inside the onload
event of the object
.
然后使用 .contentDocument.body.childNodes [0] .innerHTML
查看文本文件。
Then use .contentDocument.body.childNodes[0].innerHTML
to view the text file.
var object = document.getElementByID("data");
object.onload = function() {
var data = object.contentDocument.body.childNodes[0].innerHTML;
// use the data
};
这篇关于读取HTML对象标签中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!