读取HTML对象标签中的数据 [英] Read data in HTML object tag

查看:155
本文介绍了读取HTML对象标签中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储在服务器上的文本文件和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 在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆