Javascript循环/增加html代码,值高达55 [英] Javascript to loop / increment html code with values up to 55
本文介绍了Javascript循环/增加html代码,值高达55的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要自动在静态(.html)页面中创建链接列表,是否有使用JavaScript来完成此操作的方法。
这是我的html ...
< a href =1.html>< img src =images / 1.jpgwidth =119height =121alt =/> < / A>
这就是我想要的脚本生成55次...
< a href =1.html>< img src =images / 1.jpgwidth =119height = 121alt =/>< / a>
< a href =2.html>< img src =images / 2.jpgwidth =119height =121alt =/>< / a>
< a href =3.html>< img src =images / 3.jpgwidth =119height =121alt =/>< / a>
...等等,请给我打电话,
解决方案
这应该是个窍门:
< div id =links>
< / div>
< script>
var strLinks ='';对于(var i = 1; i< = 55; i ++){
strLinks + ='< a href ='+ i +'。html><>
, img src =images /'+ i +'。jpgwidth =119height =121alt =/>< / a>';
}
document.getElementById(links)。innerHTML = strLinks;
< / script>
JS小提琴示例: http://jsfiddle.net/RWUdG/2/
编辑:哎呀,错过了一个报价。修正。
I need to create a list of links in a static (.html) page automatically, is there a way of doing this using JavaScript. I tired a few scripts but couldn't work it out.
this is my html...
<a href="1.html"><img src="images/1.jpg" width="119" height="121" alt="" /></a>
and this is what i want the script to generate 55 times...
<a href="1.html"><img src="images/1.jpg" width="119" height="121" alt="" /></a>
<a href="2.html"><img src="images/2.jpg" width="119" height="121" alt="" /></a>
<a href="3.html"><img src="images/3.jpg" width="119" height="121" alt="" /></a>
... and so on, call me lazy but any help would be most appreciated :)
解决方案
This should do the trick:
<div id="links">
</div>
<script>
var strLinks = '';
for (var i = 1; i <= 55; i++) {
strLinks += '<a href="'+ i +'.html"><img src="images/'+ i +'.jpg" width="119" height="121" alt="" /></a>';
}
document.getElementById("links").innerHTML = strLinks;
</script>
JS Fiddle example: http://jsfiddle.net/RWUdG/2/
EDIT: Oops, missed a quote. Fixed.
这篇关于Javascript循环/增加html代码,值高达55的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文