Html 使html中的段落包含文件中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6348207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
making a paragraph in html contain a text from a file
提问by Boaz
I have an html paragraph (inside a div) in which I want to display a simple fixed text. The text is a bit long so I'd rather the text will be in a seperate txt file.
something like
我有一个 html 段落(在 div 内),我想在其中显示一个简单的固定文本。文本有点长,所以我宁愿将文本放在单独的 txt 文件中。
就像是
<div><p txt=file.txt></p></div>
Can I do something like that?
我可以做这样的事情吗?
采纳答案by Dave
You'll want to use either JavaScript or a server-side language like PHP, ASP...etc
您将需要使用 JavaScript 或服务器端语言,如 PHP、ASP...等
(supposedly can be done with HTML <embed>
tag, which makes sense, but I haven't used, since PHP...etc is so simple/common)
(据说可以用 HTML<embed>
标签完成,这是有道理的,但我没有使用过,因为 PHP...etc 是如此简单/常见)
Javascript can work:Here's a link to someone doing something similar via javascript on stackoverflow: How do I load the contents of a text file into a javascript variable?
Javascript 可以工作:这里有一个链接,指向某人在 stackoverflow 上通过 javascript 执行类似操作: 如何将文本文件的内容加载到 javascript 变量中?
PHP (as example of server-side language) is the easiest way to go though:
PHP(作为服务器端语言的例子)是最简单的方法:
<div><p><?php include('myFile.txt'); ?></p></div>
To use this (if you're unfamiliar with PHP), you can:
要使用它(如果您不熟悉 PHP),您可以:
1)check if you have php on your server
2)change the file extension of your .html file to .php
2)将 .html 文件的文件扩展名更改为 .php
3)paste the code from my PHP example somewhere in the body of your newly-renamed PHP file
3)将我的 PHP 示例中的代码粘贴到新重命名的 PHP 文件正文中的某个位置
回答by Klesun
You can do something like that in pure html using an <object>
tag:<div><object data="file.txt"></object></div>
您可以使用<object>
标签在纯 html 中执行类似的操作:<div><object data="file.txt"></object></div>
This method has some limitations though, like, it won't fit size of the block to the content - you have to specify width
and height
manually. And styles won't be applied to the text.
这种方法有一定的局限性,虽然一样,它将块的内容不适合大小-你必须指定width
和height
手动。并且样式不会应用于文本。
回答by esqew
Javascript will do the trick here.
Javascript 将在这里解决问题。
function load() {
var file = new XMLHttpRequest();
file.open("GET", "http://remote.tld/random.txt", true);
file.onreadystatechange = function() {
if (file.readyState === 4) { // Makes sure the document is ready to parse
if (file.status === 200) { // Makes sure it's found the file
text = file.responseText;
document.getElementById("div1").innerHTML = text;
}
}
}
}
window.onLoad = load();
回答by citizen conn
I would use javascript for this.
我会为此使用javascript。
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://my.remote.url/myremotefile.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4 && txtFile.status == 200) {
allText = txtFile.responseText;
}
document.getElementById('your div id').innerHTML = allText;
This is just a code sample, would need tweaking for all browsers, etc.
这只是一个代码示例,需要针对所有浏览器进行调整等。
回答by Saeed Mohtasham
Here is a javascript code I have tested successfully :
这是我测试成功的javascript代码:
var txtFile = new XMLHttpRequest();
var allText = "file not found";
txtFile.onreadystatechange = function () {
if (txtFile.readyState === XMLHttpRequest.DONE && txtFile.status == 200) {
allText = txtFile.responseText;
allText = allText.split("\n").join("<br>");
}
document.getElementById('txt').innerHTML = allText;
}
txtFile.open("GET", '/result/client.txt', true);
txtFile.send(null);
回答by Ronan Leonard
You can use a simple HTML element <embed src="file.txt">
it loads the external resource and displays it on the screen no js needed
您可以使用一个简单的 HTML 元素<embed src="file.txt">
来加载外部资源并将其显示在不需要 js 的屏幕上