如何通过首先浏览它将文本文件中的内容或html文件中的html代码加载到textarea? [英] How to load the contents in the text file or the html codes in a html-file to a textarea by browsing it first?

查看:78
本文介绍了如何通过首先浏览它将文本文件中的内容或html文件中的html代码加载到textarea?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用javascript,如何从网页打开文本文件或html文件,并将文本文件或html代码中的内容加载到textarea。请不要告诉我这不可能使用javsscript,这是可能的,但我不知道代码和我的兄弟不回家一个星期。示例代码将不胜感激。 (我已经问过这个问题,但每个人似乎都专注于问题的其他部分。)

 < html> 
< body>
< form name =abc>
< textarea name =text>在这里加载的文本< / textarea>
< input type =buttononClick =>打开文件
< / form>
< / body>
< / html>

我在论坛中发现了这个,不记得名字,这是一个工作代码,但只能加载文本文件....现在的问题是我如何才能从html文件中获取html代码。

 < html> 
< head>

< meta http-equiv =Content-Languagecontent =en-us>
< meta http-equiv =Content-Typecontent =text / html; charset = iso-8859-1>


< script type =text / javascript>

var ForReading = 1,ForWriting = 2,ForAppending = 8;
var objFSO = new ActiveXObject(Scripting.FileSystemObject);

函数loadFile(btn,obj,div,fld){
objFile = objFSO.GetFile(obj.value);
objStream = objFile.OpenAsTextStream(ForReading);
fld.value = objStream.ReadAll();
objStream.Close();
objStream = null;
fileSpecs(div,btn);
objFile = null;
返回true;
}

< / script>
< / head>

< body>
< form name =myFormonsubmit =return false;>

< textarea rows =25name =fileAreacols =70
onkeypress =return checkText(this,btnSave);>< / textarea> < / TD>


< input type =filename =fileNamesize =50
onchange =return checkFile(this,document.getElementById('fileSpec') ,btnLoad,btnSave,fileArea);>

< input type =buttonname =btnLoadvalue =Load
onclick =return loadFile(this,fileName,document.getElementById('fileSpec'),fileArea );>

< / form>
< / body>
< / html>

这是编码器声称它正在工作的另一个工具,但它不适用于我,请执行这个代码如果你有时间...如果它正在工作,请在这里提供完整的代码。
(由Ancora编码)

 < html> 
< head>
< script type =text / javascript>

函数init(){

var extText = window.frames.messageTxt.document.body.lastChild.lastChild.data;
extText = extText.replace(/ [\r\\\
] / g,);
document.forms [0] .nMessage.value = extText;
}

window.onload = init;

< / script>
< / head>
< body>
< iframe name ='messageTxt'src ='txtData.txt'style ='display:none'>< / iframe>
< form>
< textarea name ='nMessage'>< / textarea>
< input type =buttonvalue =clickonClick =init()>
< / form>
< / body>
< / html>

我猜想没有可以做到这一点...我有一个JavaScript可以加载htmlcode形式的外部html文件放入textarea。但是我丢失了那个文件,伤心没有人能够创建一个js函数(或者活动x)来执行相同的脚本功能。 解决方案 div>

使用AJAX绝对有可能。你必须记住,同源限制意味着它只能在你自己的域名上运行。一种方法是使用 jQuery.get



编辑:更完整的演示(另请参阅 jsFiddle演示):

 < html> 
< head>
< script type =text / javascriptsrc =http://code.jquery.com/jquery-1.4.2.js>< / script>
< script type =text / javascript>

函数download_to_textbox(url,el)
{
$ .get(url,null,function(data)
{
el.val(data );
},text);

$ b $(function()
{
$('#open')。click(function()
{
download_to_textbox (/ ajax_json_echo /?foo = bar,$(textarea [name ='text']));
});
});
< / script>
< / head>
< body>
< form name =abc>
< textarea name =textrows =20cols =70>在这里加载文本< / textarea>
< input id =openvalue =opentype =button>
< / form>
< / body>
< / html>


Using javascript, how to open a text-file or html-file from a web-page and load the contents in the text file or the html code to a textarea. Please Dont tell me this not possible using javsscript, it is possible but i dont know the code and my bro not coming home for a week . Example codes will be greatly appreciated. (i have already asked this question but everyone seemed to concentrate on the other part of the question.)

<html>
<body>
<form name="abc">
<textarea name="text">loaded text here</textarea>
<input type="button" onClick=""> open file
</form>
</body>
</html>

I found this in a forum , dont remember name , This is a working code but can only load text files .... now the question is how can I get it to fetch htmlcodes from a html file.

<html>
<head>

<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


<script type="text/javascript">

var ForReading = 1, ForWriting = 2, ForAppending = 8;
var objFSO = new ActiveXObject("Scripting.FileSystemObject");

function loadFile(btn, obj, div, fld) {
objFile = objFSO.GetFile(obj.value);
objStream = objFile.OpenAsTextStream(ForReading);
fld.value = objStream.ReadAll();
objStream.Close();
objStream= null;
fileSpecs(div, btn);
objFile = null;
return true;
}

</script>
</head>

<body>
<form name="myForm" onsubmit="return false;">

<textarea rows="25" name="fileArea" cols="70"
onkeypress="return checkText(this, btnSave);"></textarea> </td>


<input type="file" name="fileName" size="50"
onchange="return checkFile(this, document.getElementById('fileSpec'), btnLoad, btnSave, fileArea);">

<input type="button" name="btnLoad" value="Load" 
onclick="return loadFile(this, fileName, document.getElementById('fileSpec'), fileArea);"> 

</form>
</body>
</html>

This is another one the coder claims it to be working but it is nt working for me , please execute this code if u hav time ...if it is working please provide the complete code here. (coded by Ancora)

<html>
<head>
<script type="text/javascript">

function init(){

var extText = window.frames.messageTxt.document.body.lastChild.lastChild.data;
extText = extText.replace(/[\r\n]/g," ");
document.forms[0].nMessage.value = extText;
}

window.onload=init;

</script>
</head>
<body>
<iframe name='messageTxt' src='txtData.txt'  style='display:none'></iframe>
<form>
<textarea name='nMessage'></textarea>
<input type="button" value="click" onClick="init()">
</form>
</body>
</html>

i guess no can do this...i had a javascript that can load htmlcode form an external html file into the textarea. But i lost that file, sad no one here has the scripting abilities to create a js function (or active x) that can do the same.

解决方案

It's definitely possible, using AJAX. You have to keep in mind that same-origin restrictions mean it will only work on your own domain. One way is with jQuery.get:

EDIT: A more complete demo (see also jsFiddle demo):

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.js"></script>
<script type="text/javascript">

    function download_to_textbox(url, el)
    {
      $.get(url, null, function(data)
                       {
                         el.val(data);
                       }, "text");
    }

    $(function()
    {
      $('#open').click(function()
      {
        download_to_textbox("/ajax_json_echo/?foo=bar", $("textarea[name='text']"));
      });
    });
</script>
</head>
<body>
    <form name="abc">
        <textarea name="text" rows="20" cols="70">loaded text here</textarea>
        <input id="open" value="open" type="button">
    </form>
</body>
</html>

这篇关于如何通过首先浏览它将文本文件中的内容或html文件中的html代码加载到textarea?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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