将li附加到另一个html文件中的现有ul中

时间:2012-02-08 09:20:04

标签: javascript jquery

我想将li添加到现有的ul中,例如:jQuery: how to add <li> in an existing <ul>?

但在我的代码中,li是来自另一个html文件的现有li。 所以我的products.html有一个ul。在这个html文件中,我想附加li

<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
});

那我该怎么办?

1 个答案:

答案 0 :(得分: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!
});
相关问题