我想用 js 打印一个数组,然后用 html() 向每个元素添加一些数据
我使用的代码是:
<script type="text/javascript">
$(document).ready(function() {
var testArray = ["test1","test2","test3","test4"];
for(var i=0;i<testArray.length;i++){
document.write(" " +testArray[i]+"<br />").html("is the best");
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
HTML:
<div id="myDIV"></div>
Run Code Online (Sandbox Code Playgroud)
JS:
$(document).ready(function() {
var testArray = ["test1","test2","test3","test4"];
var vPool="";
jQuery.each(testArray, function(i, val) {
vPool += val + "<br /> is the best <br />";
});
//We add vPool HTML content to #myDIV
$('#myDIV').html(vPool);
});
Run Code Online (Sandbox Code Playgroud)
更新:添加演示链接:http : //jsfiddle.net/aGX4r/43/