简体   繁体   English

我的jQuery函数只能第一次运行

[英]My jQuery function can only operate the first time

I have try to make a custom script to show shopping cart.我尝试制作一个自定义脚本来显示购物车。 I think I have nearly finished but I have a problem with it where I can only call the function once.我想我已经快完成了,但是我遇到了一个问题,我只能调用一次该函数。

On http://articles-authors.com/order-form/ you can see the cart in right top and when the website load a function has been load to create the cart.http://articles-authors.com/order-form/上,您可以在右上角看到购物车,并且当网站加载时,已经加载了一个功能来创建购物车。 But when the try to hover over the cart nothing happen and there should it call another function to recreate the cart and set the class a active class to the html element #cart .但是,当尝试将鼠标悬停在购物车上时,什么也没有发生,它应该调用另一个函数来重新创建购物车并将类设置为 html 元素#cart的活动类。

Here is the code:这是代码:

First the function to call the cart create function首先调用购物车创建函数的函数

makeCartWithoutActive();
$("#cart").mouseenter(function(){
    makeCartWithActive();
    }).mouseleave(function(){
    makeCartWithoutActive();
});

The reason is using mouseenter is because I use jQuery 2.0.3 and live has been remove in 1.7 I think.使用mouseenter的原因是因为我使用 jQuery 2.0.3 并且我认为 live 已在 1.7 中删除。

Here you can see the function there should recreate the cart and then set the class active to #cart so the cart will show:在这里你可以看到那里的函数应该重新创建购物车,然后将活动类设置为#cart ,这样购物车就会显示:

function makeCartWithActive(){
$.get("http://articles-authors.com/order-form/cart/get",function(data) {
  var html = "";
    if(data.msg === false){
      html = '<div id="cart" class="right active">'+
            '<div class="heading"><a><span id="cart-total">0 item(s) - $0.00</span></a></div>'+
            '<div class="content"><div class="empty">'+data.respone+'</div></div></div>';
     $("#cart").replaceWith(html);
  
     }else{
     
     html = '<div id="cart" class="right active"><div class="heading"><a><span id="cart-total">'+data.totalitem+' item(s) - $'+data.totalprice+'</span></a></div>'+
            '<div class="content"><div class="mini-cart-info"><table><tbody>';

    $.each(data.data, function(i, currProgram) {
         $.each(currProgram, function(key,val) {
            html += '<tr><td class="name">'+val.name+'</td><td class="quantity">x '+val.quantity+
                    '</td><td class="total">$'+val.price+'</td><td class="remove"><img src="http://articles-authors.com/order-form/img/remove-small.png'+
                    '" onclick="RemoveItem('+val.rowid+');" width="12" height="12" title="Remove" alt="Remove"></td></tr>';
        });
    });
        
      html += '</tbody></table></div>'+
            '<div class="mini-cart-total"><table><tbody><tr><td align="right"><b>Sub-Total:</b></td><td align="right">'+data.totalprice+'</td></tr><tr><td align="right"><b>Total:</b></td><td align="right">'+data.totalprice+'</td></tr></tbody></table></div>'+
            '<div class="checkout"><a class="button" href="http://articles-authors.com/order-form/cart">Checkout</a></div></div></div>';
            
     
     $("#cart").replaceWith(html);
 }},

"jsonp");

}

Hope someone can help me.希望可以有人帮帮我。 I'm still new to jQuery/JavaScript so have no idea why this happen.我还是 jQuery/JavaScript 的新手,所以不知道为什么会这样。

Use delegated event handlers使用委托事件处理程序

$(document).on('mouseenter', '#cart', function(){
    makeCartWithActive();
});

$(document).on('mouseleave', '#cart', function(){
    makeCartWithoutActive();
});

Or you can use或者你可以使用

$(document).on({
    "mouseenter" : function(e) { makeCartWithActive(); },
    "mouseleave" : function(e) { makeCartWithoutActive(); }
}, "#cart");
$(document).on('mouseenter', '#cart', function(){
    makeCartWithActive
}).on('mouseleave', '#cart', function(){
    makeCartWithoutActive
});

you're replacing the element, so the handler disappears.您正在替换元素,因此处理程序消失了。 You need to delegate the function to the closest static parent element.您需要将该函数委托给最近的静态父元素。

I choose document, but for performance, you should choose something closer to #cart我选择文档,但为了性能,您应该选择更接近#cart的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM