我試圖通過JQuery循環來增加一些HTML代碼與JSON文件中的數據。問題是,我似乎無法弄清楚如何將div寫入到下面的HTML中,而是循環將數據相互遞增,最後在屏幕上顯示數組中的最後一個對象。使用循環創建新div的JQuery
這裏是我的Javascript(tubing.js):
$(document).ready(function() {
crazyFun();
});
function crazyFun() {
for (var i = 0; i < virtualGoods.length; i++) {
var alpha= $('div.container').clone();
alpha.find('div.picture').attr('id', virtualGoods[i].picture);
alpha.find('h2').html(virtualGoods[i].header);
alpha.find('p').html(virtualGoods[i].text);
alpha.find('div.notification').attr('id', virtualGoods[i].notification);
$('div.container').append(alpha);
}
}
}
「集裝箱」 是HTML DIV,我想我的網頁上重複使用填充JSON數據。
這裏是我的HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="iphone.css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="virtualgoods.js"></script>
<script type="text/javascript" src="tubing.js"></script>
</head>
<body>
<div id="mainhead">
<h1>
Hello World
</h1>
</div>
<div id="repeater">
<div class="container">
<div class="picture">
</div>
<div class="object">
<div class="header">
<h2></h2>
</div>
<div class="text">
<p>
</p>
</div>
</div>
<div class="notification">
</div>
<div class="neg">
</div>
</div>
</div>
<div id="buffer">
</div>
</body>
</html>
你可以顯示你的html代碼? – 2010-07-20 04:51:37
你能澄清哪一個div被克隆了嗎?它被放置在哪裏...是被克隆並插入緩衝區容器的轉發容器?當我確切地知道什麼是繼續 – 2010-07-20 05:08:06
當然,Alastair。我需要克隆Container類div中的所有內容,並且需要在前一個(一個接一個)下面創建額外的Container div。緩衝區div僅僅是一個虛擬div ...我只是意識到我需要擺脫標籤中的Container類屬性 我也試着實現你的代碼,當我運行HTML文件時,它減慢了我的計算機的速度。 – Sachin 2010-07-20 05:16:55