html5中文学习网

您的位置: 首页 > 网站及特效实例 > jquery特效 » 正文

如何逐行读取txt文本?_编程语言综合

[ ] 已经帮助:人解决问题

 如何逐行读取txt文本?Ajax可以实现么Mh4HTML5中文学习网 - HTML5先行者学习网

想要读取分行txt文件,并在每行前后添加内容。Mh4HTML5中文学习网 - HTML5先行者学习网

如txt文件内容是这样的Mh4HTML5中文学习网 - HTML5先行者学习网

http://www.baidu.com/http://www.google.com.hk/

我想最终输出的结果是Mh4HTML5中文学习网 - HTML5先行者学习网

<a href="http://www.baidu.com/"><img src="1.jpg" /></a><a href="http://www.google.com.hk/"><img src="2.jpg" /></a>

就是这样遍历…请问怎样实现?Mh4HTML5中文学习网 - HTML5先行者学习网

我在W3School找到一段代码,直接读取txt文件的,不能添加前后缀。Mh4HTML5中文学习网 - HTML5先行者学习网

<html><head><script type="text/javascript">function loadXMLDoc(){var xmlhttp;if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.open("GET","/ajax/test1.txt",false);xmlhttp.send();document.getElementById("myDiv").innerHTML=xmlhttp.responseText;}</script></head><body><div id="myDiv"><h2>Let AJAX change this text</h2></div><button type="button" onclick="loadXMLDoc()">通过 AJAX 改变内容</button></body></html>

求大神相助,谢谢。Mh4HTML5中文学习网 - HTML5先行者学习网


解决方案:Mh4HTML5中文学习网 - HTML5先行者学习网

 Mh4HTML5中文学习网 - HTML5先行者学习网

<html><head><script type="text/javascript">function loadXMLDoc(){var xmlhttp;if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.open("GET","/ajax/test1.txt",false);xmlhttp.send();var i=1;document.getElementById("myDiv").innerHTML=xmlhttp.responseText.replace(/.+/g,function(url){  return '<a href="'+ url +'"><img src="'+ i++ +'.jpg" /></a>'})}</script></head><body><div id="myDiv"><h2>Let AJAX change this text</h2></div><button type="button" onclick="loadXMLDoc()">通过 AJAX 改变内容</button></body></html>
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助