如何将数组中的表排列到原型中

How to Arrange Table in Array to Prototype?

本文关键字:排列 原型 数组      更新时间:2023-09-26

所以,我想修改数组,使它在表中显示它的内容,一行一行,一次。随着下面的代码,它显示代码逐步每个元素出现不止一次。

如何解决这个问题?thx .

$(function() {
Array.prototype.ToTable=function(){
tabelul="<center><table border><tr>";
for(a=0; a<this.length;a++){
this[a]=tabelul+="<td> <div class='Expresion'>" + this[a] + "</div></td></tr>"
}

}
vaz=['one', 'two', 'three','four'];

vaz.ToTable();
$('#show').html(vaz)
})
<div id="show"></div>

我相信你想做的事情是这样的:

for(a=0; a<this.length;a++){
 tabelul+="<tr><td><div class='Expresion'>" + this[a] + "</div></td></tr>";
}

您应该构建tabelul变量,然后返回它。

下面是一个这样做的例子

这是一个演示:http://jsfiddle.net/2zchT/2/

html:

<div id="show"></div>

js:

$(function() {
 Array.prototype.ToTable=function(){
  var tabelul = "<center><table border>";
  for(var a=0; a<this.length;a++){
   tabelul += "<tr><td><div class='Expresion'>" + this[a] + "</div></td></tr>"
  }
  tabelul += "</table></center>";
  return tabelul;
 };
 vaz=['one', 'two', 'three','four'];
 $('#show').html(vaz.ToTable())
})

编辑

您应该在构建表之前将这些数据植入。

var Links= new Array(); 
Links[0]='<a href="http://location.com/"><img src="http://photo1.com/"></a>'; 
Links[1]='<a href="http://location.com/"><img src="http://photo2.com/"></a>'; 
Links[2]='<a href="http://location.com/"><img src="http://photo3.com/"></a>'; 
Links[3]='<a href="http://location.com/"><img src="http://photo4.com/"></a>'; 
Links[4]='<a href="http://location.com/"><img src="http://photo5.com/"></a>';
var vaz=['one', 'two', 'three','four'];
for( var i = 0; i < vaz.length; i++ )
{
 vaz[i] = Links[i] + vaz[i];
}

下面是一个演示:http://jsfiddle.net/2zchT/4/