我正在构建一个菜单订购应用程序,我必须在其中使用 Jquery。
我正在使用该clone()方法复制带有弹出必要数据的购物车项目。它工作一次,然后记录并object用<prototype>.
我正在克隆的是我的 HTML 中的一个部分,我将其用作模板并id隐藏它。我在克隆的项目上删除了这个。
弹出我排除的数据,因为它工作正常并且功能在其他文件中,但我对它们是原因的想法持开放态度。
HTML:
        <div class="cart-item" id="cartitem-template">
          <div class="left">
            <div class="image"></div>
            <div class="price"></div>
          </div>
          <div class="mid">
            <div class="name">
            </div>
            <div class="stars">
              <span></span>
            </div>
            <div class="underline"></div>
            <div class="toggleReviews">
              <span></span>
            </div>
          </div>
          <div class="right">
            <div class="remove-cart-item">✕</div>
          </div>
        </div>
      </div>
JS函数:
    buildCartItem = function(menuItem) {
        const template = $("#cartitem-template")
        template
            .removeAttr("id")
            .clone(false)
        const newCartItem = template
        newCartItem.insertAfter(".cart-item")
        console.log(newCartItem)
        //Get object and index atts from clicked menu item
        //Also set that same data into the dom of the cart item
        //As well as set that data to local storage
        ...
        // Apply data to the cloned cart item template
        newCartItem.find(".image").css("background-image", "url(" + data.image + ")")
        setData(".price", "$"+data.price, newCartItem)
        ...
    }
    $(".menu-item").click(function() {
        buildCartItem($(this))
    })
我使用.clone()正确吗?老实说卡住了