A. javascript如何读取txt文本内容
<!DOCTYPEhtml>
<html>
<head>
<title>文件示例</title>
<metaname="name"content="content"charset="utf-8">
</head>
<body>
<inputtype="file"id="file"/>
<inputtype="button"onclick="readText()"value="FileButton">
<divid="tt">
</div>
</body>
</html>
<scriptcharset="utf-8">
window.onload=function(){
if(typeof(FileReader)=="undefined")
{
alert("你的浏览器不支持文件读取");
document.write("");
}else
{
alert("你的浏览器支持文件读取");
}
}
functionreadText(){
varfile=document.getElementById("file").files[0];
varreader=newFileReader();
reader.readAsText(file);
reader.onload=function(data)
{
vartt=document.getElementById("tt")
tt.innerHTML=this.result;
}
}
</script>
B. 如何用js获取外部脚本的文件内容
您需要的功能可以利用AJAX来实现。下面是我写的代码读取一个文件并显示出来,供您参考。
<!doctypehtml>
<html>
<head>
<metacharset="utf-8">
<title>使用AJAX读取外部文件</title>
</head>
<body>
<divid="txt">外部文件内容</div>
<script>
if(window.ActiveXObject)//windows系统
xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
elseif(window.XMLHttpRequest)//其它系统,及非IE6系统
xmlHttp=newXMLHttpRequest();
xmlHttp.onreadystatechange=Callback;
xmlHttp.open("GET","mytxt.txt",true);//读取mytxt.txt内容
xmlHttp.send(null);
functionCallback()
{
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xml=xmlHttp.responseText;//读取返回值即文件内容
document.getElementById("txt").innerText=xml;//在当前页面显示文件内容
}
}
}
</script>
</body>
</html>
C. 如何用js获取id 元素内容
可以使用js的attr方法实现用js获取id元素内容。
具体步骤如下:
需要准备的材料分别是:电脑、浏览器、ultraedit。
1、在ue编辑器中新建一个空白的html文件,js文件。
D. javascript怎么获取元素的所有内容
试一下ContentEditable这个属性。
看一下这个 Demo:
HTML5 Demo: ContentEditable
http://html5demos.com/contenteditable
相关代码:
vareditable=document.getElementById('editable');addEvent(editable,'blur',function(){
//lamethatwe'rehookingtheblurevent
localStorage.setItem('contenteditable',this.innerHTML);
document.designMode='off';});addEvent(editable,'focus',function(){
document.designMode='on';});if(localStorage.getItem('contenteditable')){
editable.innerHTML=localStorage.getItem('contenteditable');}
不过,建议用JQuery比较好,而且简单,Jquery有个属性,直接可以获取当前元素的所有子元素
E. 在js中怎样获取当前页面表格中的数据
1、JS获取表格的简便方法:获取tbody:tBodies 获取thead:tHead 获取tfoot:tFoot 获取行tr:rows 获取列td:cells
使用实例: oTable.tBodies[0] oTable.tHead[0] oTable.tFoot[0] oTable.rows[1] oTable.cells[1]
2、遍历
var oTable=document.getElementById("表格id");
oTable.tBodies[0]可以看成是rows和cells组成的二维数组,用两个for循环嵌套遍历一下就可以了
F. 如何通过js来读取txt当中的内容,求详细有demo更好,谢谢了·!
<script language="javascript" type="text/javascript">
/*
object.OpenTextFile(filename[, iomode[, create[, format]]])
参数
object
必选项。object 应为 FileSystemObject 的名称。
filename
必选项。指明要打开文件的字符串表达式。饥毕
iomode
可选项。可以是三个常数之一:ForReading 、 ForWriting 或 ForAppending 。
create
可选项。Boolean 值,指明当指定的 filename 不存在时是否创建新文件。如果创建新文件则值为 True ,如果不创建则早磨为 False 。如果忽略,则不创建新文件。
format
可选项。使用三态值中的一个来指明打开文件的格式。如果忽略,那么文件将以 ASCII 格式打开。
设置
iomode 参烂睁芹数可以是下列设置中的任一种:
常数 值 描述
ForReading 1 以只读方式打开文件。不能写这个文件。
ForWriting 2 以写方式打开文件
ForAppending 8 打开文件并从文件末尾开始写。
format 参数可以是下列设置中的任一种:
值 描述
TristateTrue 以 Unicode 格式打开文件。
TristateFalse 以 ASCII 格式打开文件。
TristateUseDefault 使用系统默认值打开文件。
*/
//读文件
function readFile(filename){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filename,1);
var s = "";
while (!f.AtEndOfStream)
s += f.ReadLine()+"\n";
f.Close();
return s;
}
//写文件
function writeFile(filename,filecontent){
var fso, f, s ;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.OpenTextFile(filename,8,true);
f.WriteLine(filecontent);
f.Close();
alert('ok');
}
</script>
<html>
<input type="text" id="in" name="in" />
<input type="button" value="Write!"
onclick="writeFile('F:/Appserv/www/12.txt',document.getElementById('in').value);"/><br><br>
<input type="button" value="Read!"
onclick="document.getElementById('show').value=readFile('F:/Appserv/www/12.txt');"/><br>
<textarea id="show" name="show" cols="100" rows="20" >
</textarea>
</html>
G. 如何用js获取其它页面的内容
用jquery的load方法加载,
例子1(点击按钮加载一个注册页面的所有内容装入div容器回):
$("button").click(function(){
$("div").load('regist.html');
});
例子2(点击按钮答加载一个注册页面里面的ID为box的标签里面的所有内容装入div容器):
$("button").click(function(){
$("div").load("regist.html #box");
});
H. 如何用JS读取html文件中的内容到另外的一个HTML页面上
1、首先新建文件。
注意事项:
因为Netscape与Sun合作,Netscape管理层希望它外观看起来像Java,因此取名为JavaScript。但实际上它的语法风格与Self及Scheme较为接近。
I. 在JS脚本中怎样读取数据库中的数据
js是无法直接读取 数据库的
只能用服务器端脚本与js搭配使用
如:
<% String testStr = "HelloJava";%>
<script>
alert('<%=testStr%>');
</script>
因为服务端脚本是先运行的,Js是后运行在客户端的。
所以可以先用服务端脚本读取数据库数据 然后放到js里再运行。
J. js怎么获取文本框内容
用js的value方法就可以了。下面是代码,仅供参考:
<body>
<inputtype='text'id='inp'/>
<inputtype='button'id='btn'value='click'/>
</body>
<script>
varoInp=document.getElementById('inp');
varoBtn=document.getElementById('btn');
oBtn.onclick=function(){
alert(oInp.value);//这里就可以版获取文本框里的值了。权
};
</script>