[英]How to load the contents in the text file or the html codes in a html-file to a textarea by browsing it first?
使用javascript,如何從網頁打開文本文件或html文件,以及如何將文本文件或html代碼中的內容加載到文本區域。 請不要告訴我使用javsscript無法實現,這是可能的,但是我不知道代碼,而我的兄弟已經一周沒有回家了。 示例代碼將不勝感激。 (我已經問過這個問題,但每個人似乎都專注於問題的另一部分。)
<html>
<body>
<form name="abc">
<textarea name="text">loaded text here</textarea>
<input type="button" onClick=""> open file
</form>
</body>
</html>
我在論壇上找到了這個,不記得名字了,這是一個有效的代碼,但是只能加載文本文件..現在的問題是,如何獲取它以從html文件中獲取htmlcode。
<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>
這是編碼器聲稱正在工作的另一個代碼,但對我而言不起作用,如果您有空,請執行此代碼...如果工作,請在此處提供完整的代碼。 (由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>
我猜沒有辦法...我有一個JavaScript,可以將htmlcode從外部html文件加載到textarea中。 但是我丟失了那個文件,可惜這里沒有人具有腳本功能來創建可以執行相同操作的js函數(或活動x)。
使用AJAX絕對有可能。 您必須記住,同源限制意味着它僅適用於您自己的域。 一種方法是使用jQuery.get :
編輯:更完整的演示(另請參閱jsFiddle演示 ):
<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>
上面的答案很好用,但是您必須了解他們在進行自定義。 這是最簡單的解決方案(自定義上述解決方案),已在IE8上進行了測試,並且工作正常(請記住,您必須在iframe中查看HTML渲染,才能知道src
屬性正確無誤):
<html>
<head>
<script type="text/javascript">
function init(){
var extText = window.frames[0].document.getElementsByTagName("HTML")[0].outerHTML;
document.getElementById("nMessage").innerText = extText;
}
</script>
</head>
<body>
<iframe name="messageTxt" src="[place the path to your html file here]" ></iframe>
<form>
<textarea name="nMessage" id="nMessage" cols="100" rows="20"></textarea>
<input type="button" value="click" onClick="init()" />
</form>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.