读文件用Fso:基本用法 var fso = new ActiveXObject("Scripting.FileSystemObject"); (其他不知道的去baidu上搜索)当你读到数据后,再用 document.getElementById("content").value=你读到的东西基本思路很简单,自己动手做做,不会再来说,这样才有人会帮你
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script language="javascript"> function getLenght() {return document.getElementById("content").value.length; } //混合内容 function mixContent() { var j=0; var mixStr; var Rndn; while(i<5){ mixStr=getContent(); Rndn=Math.floor(Math.random()*getLenght()+1); mixStr=mixStr.substring(0,Rndn)+getInsertContent()+mixStr.substring(Rndn,getLenght()); } document.getElementById("content").value=mixStr; } //获取Content里面本来有的内容 function getContent() { return document.getElementById("content").value; }//获取要插入行数据 function getInsertContent() { var fText; var RndNum; var i=0; var fso = new ActiveXObject("Scripting.FileSystemObject"); txtFile = fso.OpenTextFile("insert.txt", 1, false, 0); RndNum=Math.floor(Math.random()*5+1); //5条测试 while (i<=RndNum) { txtFile.SkipLine(); } fText += txtFile.ReadLine(); return fText txtFile.Close(); fso = null }</script> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Test</title> </head><body> <form id="form1" name="form1" method="post" action="">
http://www.never-online.net/blog/article.asp?id=113
大家一起研究一下!
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function getLenght()
{return document.getElementById("content").value.length;
}
//混合内容
function mixContent()
{
var j=0;
var mixStr;
var Rndn;
while(i<5){
mixStr=getContent();
Rndn=Math.floor(Math.random()*getLenght()+1);
mixStr=mixStr.substring(0,Rndn)+getInsertContent()+mixStr.substring(Rndn,getLenght());
}
document.getElementById("content").value=mixStr;
}
//获取Content里面本来有的内容
function getContent()
{
return document.getElementById("content").value;
}//获取要插入行数据
function getInsertContent()
{
var fText;
var RndNum;
var i=0;
var fso = new ActiveXObject("Scripting.FileSystemObject");
txtFile = fso.OpenTextFile("insert.txt", 1, false, 0);
RndNum=Math.floor(Math.random()*5+1); //5条测试
while (i<=RndNum)
{
txtFile.SkipLine();
}
fText += txtFile.ReadLine();
return fText
txtFile.Close();
fso = null
}</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Test</title>
</head><body>
<form id="form1" name="form1" method="post" action="">
<textarea id="content" cols="137" rows="20" name="textarea"></textarea>
<input name="mix" id="mix" type="button" value="混合" onclick="mixContent()" />
</form>
</body>
</html>
简单的我也是可以写出来的 但是就是得不到想要的效果
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function getLenght()
{return document.getElementById("content").value.length;
}
//混合内容
function mixContent()
{
var j=0;
var mixStr;
var Rndn;
while(i<5){
mixStr=getContent();
Rndn=Math.floor(Math.random()*getLenght()+1);
mixStr=mixStr.substring(0,Rndn)+getInsertContent()+mixStr.substring(Rndn,getLenght());
}
document.getElementById("content").value=mixStr;
}
//获取Content里面本来有的内容
function getContent()
{
return document.getElementById("content").value;
}//获取要插入行数据
function getInsertContent()
{
var fText;
var RndNum;
var i=0;
var fso = new ActiveXObject("Scripting.FileSystemObject");
txtFile = fso.OpenTextFile("insert.txt", 1, false, 0);
RndNum=Math.floor(Math.random()*5+1); //5条测试
while (i<=RndNum)
{
txtFile.SkipLine();
}
fText += txtFile.ReadLine();
return fText
txtFile.Close();
fso = null
}</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Test</title>
</head><body>
<form id="form1" name="form1" method="post" action="">
<textarea id="content" cols="137" rows="20" name="textarea"></textarea>
<input name="mix" id="mix" type="button" value="混合" onclick="mixContent()" />
</form>
</body>
</html>
简单的我也是可以写出来的 但是就是得不到想要的效果
while (i<=RndNum)
{
txtFile.SkipLine();//i没变化,会死循环的
}
fText += txtFile.ReadLine();
return fText;//在这里return了,后面的close就没意义了
txtFile.Close();//移到return前面去
fso = null;//移到return前面去
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function getLenght()
{return document.getElementById("content").value.length;
}
//混合内容
function mixContent()
{
var j=0;
var mixStr;
var Rndn;
while(j<5){
mixStr=getContent();
Rndn=Math.floor(Math.random()*getLenght()+1);
mixStr=mixStr.substring(0,Rndn)+getInsertContent()+mixStr.substring(Rndn,getLenght());
j+=1;
}
document.getElementById("content").value=mixStr;
}
//获取Content里面本来有的内容
function getContent()
{
return document.getElementById("content").value;
}//获取要插入行数据
function getInsertContent()
{
var fText;
var RndNum;
var i=0;
var fso = new ActiveXObject("Scripting.FileSystemObject");
txtFile = fso.OpenTextFile("insert.txt", 1, false, 0);
RndNum=Math.floor(Math.random()*5+1); //5条测试
while (i<=RndNum)
{
i+=1;
txtFile.SkipLine();
}
fText += txtFile.ReadLine();
txtFile.Close();
fso = null
return fText}</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Test</title>
</head><body>
<form id="form1" name="form1" method="post" action="">
<textarea id="content" cols="137" rows="20" name="textarea"></textarea>
<input name="mix" id="mix" type="button" value="混合" onclick="mixContent()" />
</form>
</body>
</html>
大头虾!忘了
但是为什么我在 firebug中调试显示一下错误?
ActiveXObject is not defined
getInsertContent()Untitled-1.html (行 36)
mixContent()Untitled-1.html (行 19)
onclick(click clientX=41, clientY=294)mbh6XUrh...39w%3D%3D (行 2)
[Break on this error] var fso = new ActiveXObject("Scripting.FileSystemObject");
不知道你有没有安装visual studio,在IE中开启javascript调试,然后在需要调试的地方,加上debugger;就会调用vs来进行JS调试了,JS出错也会调用
RndNum=Math.floor(Math.random()*2+1); //2条测试 修改了 可以运行 但是为什么是undefined4
我的txt文件是
1
2
3
4
5
不破坏html机构的方法!
---------
什么意思??