2012-02-08 42 views
0

我喜歡的李追加到現有的UL喜歡這裏現有UL:jQuery: how to add <li> in an existing <ul>?追加李進內的另一個HTML文件

但在我的代碼裏是從另一個HTML文件的現有華里。 所以我有一個ul。的products.html。在這個HTML文件我喜歡

<li class="first-li" id="hotel_belle"> 
    <h2>Hotel Belle</h2> 
    <div> 
     <img class="sub-img" alt="picture from hotel" src="pic/products/HotelBelle.png" /> 
     <p>Lorem Ipsum</p> 
     <a href="#"><img class="btn-cart" alt="add this product to cart" src="pic/book/cart.png" /></a> 
    </div>  
</li> 

追加李進cart.html:

<div id="inner-cart-content"> 
     <ul id="content-cart"> 
      <li> 
       //The new li element from products.html 

在JavaScript我寫這樣的:

$(".btn-cart").click(function(){ 
var cart = $(this).parents("li").eq(0); 

//$("#inner-cart-content ul").append('cart'); <--this is not working 
}); 

所以我有什麼要做?

+0

在您的註釋行中,您執行'$(「#inner-merke-content ul」)',但您的UL ID是「inner - ** cart ** - content」。此外,如何顯示頁面「products.html」?在iframe中?通過ajax加載? – 2012-02-08 09:25:23

+0

哦對不起,我編輯了這個錯誤:) 它不是一個iframe - 只是一個正常的html文件 - 第一次我想實現一個沒有內容加載ajax的原型 - 所以李元素是硬編碼內容 – Jules 2012-02-08 09:32:46

+0

如果購物車,您要放入商品的位置不會顯示在與產品相同的網站上,那麼您將需要其他解決方案。 – 2012-02-08 09:42:21

回答

0
$(".btn-cart").click(function(){ 
var item = //grab what you want 

$("#content-cart ul").append(item); 
$("<div id="inner-cart-content">").show(); //After adding the item, show the cart 
$("div").delay(4000).hide(); And after 4 seconds hide it again! 
}); 
+0

與此我得到一個空白頁!? – Jules 2012-02-13 13:13:23