如何在对象元素中获取文本? [英] How can I get the text inside an object element?

查看:116
本文介绍了如何在对象元素中获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ID为x的对象元素中,一个文本文件已加载并正确显示.如何使用JavaScript获取此文本?

In the object element with the ID x a text file is loaded and displayed correctly. How can I get this text with JavaScript?

我设置了

y.data = "prova.txt"

然后尝试

y.innerHTML;
y.text;
y.value;

这些工作都没有.

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <object id="x" data="foo.txt"></object>
    <script>
      var y = document.getElementById("x")
    </script>
  </body>
</html>

推荐答案

恐怕这并非如您所愿那样容易.

I'm afraid this isn't going to be easy as you'd like it to be.

根据您的评论,您首先尝试了AJAX,但是遇到了CORS问题.当您尝试包含来自不同域名上文件的数据时,就会发生这种情况.

According to your comments, you tried AJAX first, but came across CORS problems. This happens when you try to include data from files on a different domain name.

由于此操作无效,因此您尝试将文件包含在object标记内.效果类似于iframe-数据将显示在网页上,但是由于与上述相同的原因,如果文件位于其他域名下,则您无法通过JavaScript访问数据 .这是一项安全功能.这就解释了您最近遇到的错误:

Since that didn't work, you tried to include the file inside an object tag. This works a bit like an iframe - the data will be displayed on the webpage, but for the same reasons as above, you cannot access the data through JavaScript if the file is under a different domain name. This is a security feature. That explains the error you were getting most recently:

未捕获的SecurityError:无法从"HTMLObjectElement"读取"contentDocument"属性

Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLObjectElement'


现在,有几种方法可以解决此问题.


Now, there are a few ways you might be able to get around this.

首先,如果这是专门供您自己使用的程序,则可以在禁用网络安全性的情况下启动浏览器(尽管这通常对于浏览网络是危险的).例如,在Chrome中,您可以通过使用--disable-web-security标志启动Chrome来执行此操作. 此处有更多详细信息.

Firstly, if this is a program exclusively for your own use, you can start your browser with web-security disabled (though this is dangerous for browsing the web generally). In Chrome, for example, you can do this by launching Chrome with the --disable-web-security flag. More details here.

第二,您可以尝试安排您的文档和文件确实属于同一域.如果您可以控制文件,则可能只能执行此操作.

Secondly, you can try to arrange that your document and the file do belong under the same domain. You will probably only be able to do this if you have control of the file.

您的错误消息(特别是a frame with origin "null")使我认为您是直接在Web浏览器中而不是通过服务器运行文件.如果您经过一台实际的服务器,它可能会使事情变得更好.

Your error message (specifically a frame with origin "null") makes me think that you are running the files directly in the web-browser rather than through a server. It might make things work better if you go through an actual server.

如果您安装了Python(Linux和Mac附带),最简单的方法是打开终端并浏览到代码目录.然后启动一个简单的Python服务器:

If you've got Python installed (it's included on Linux and Mac), the easiest way to do that is to open up the terminal and browse to your code's directory. Then launch a simple Python server:

cd /home/your_user_name/your_directory
python -m SimpleHTTPServer

这将启动Web服务器,您可以通过导航到http://localhost:8000/your_file.html来在浏览器中对其进行访问.

That will start up a web server which you can access in your browser by navigating to http://localhost:8000/your_file.html.

如果您在Windows上并且尚未安装Python,则还可以使用内置的 WAMP (或只需安装 Python ).

If you are on Windows and haven't got Python installed, you could also use the built-in IIS server, or WAMP (or just install Python).

这篇关于如何在对象元素中获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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