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

append li into an existing ul inside another html file

本文关键字:ul 文件 html li 另一个      更新时间:2023-09-26

我喜欢将li附加到现有的ul中,如下所示:jQuery:如何添加<李>在现有<ul>?

但在我的代码中,li是另一个html文件中的现有li。所以我有一个带ul的products.html。在这个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
});

那我该怎么办?

$(".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!
});