2014-07-18 66 views
0

的div我使用jQuery使用下面的代碼的頁面中創建多個產品創建標籤..各地使用jQuery

$("#product-container").empty() 
    slicedata.forEach(function(e,i,a){ 
     var obj = e; 
     i = parseInt(obj.id) 
     $("<a href = /product >").appendTo('#product-container') 
     $("<div id = productid" + i + " class = product-cards </div>").appendTo('#product-container') 
     $("<div id='product" + i + "left' class='product-cards-left' style='background-image:url( " + imagepath_start + obj.image_caption + ")'> </div>").appendTo('#productid' + i); 
     $("<div id = product" + i + "right class = product-cards-right> </div>").appendTo('#productid' + i ) 
     $("<label><b> Price: <b></label> <label>" + '$' + obj.price + "</label><br>").appendTo('#product' + i +"right" ) 
     $("<label><b> Old Price: <b></label> <label>" + '$' + obj.old_price + "</label><br>").appendTo('#product' + i +"right" ) 
     $("<label><b> Author Name: <b></label> <label>" + obj.author_name + "</label>").appendTo('#product' + i +"right" ) 
     $("<div id= elementid style='display:none' >"+ obj.id+" </div>").appendTo('#product' + i +"right" ) 
    }) 

現在,我需要添加周圍的div的<a>標籤,使當用戶點擊產品卡片時,他將被帶到詳細信息頁面...

我使用<a>標籤來構建每個產品使用下面的stmt,但它創建了<a>並立即關閉它..我需要在所有d後關閉<a> IVS(即;產品被創建)。

$("<a href = /product >").appendTo('#product-container') 
+0

見http://api.jquery.com/wrap/ – j08691

+0

不難簡單地將字符串放入並在創建時手動包裝它。製作太多的'追加',因爲它是 – charlietfl

回答

1

您可以使用$.wrap這個。

$("<div id='productid" + i + "' class='product-cards'></div>") 
.appendTo('#product-container') 
.wrap("<a href='/product'></a>"); 

http://jsfiddle.net/3Kfj3/1/

+0

我試着用這個小提琴,但看起來像它不工作 - http://jsfiddle.net/3Kfj3/ – user1050619

+0

你將不得不包裝最後它變成了 – dave