【问题标题】:Getting .live or .delegate or livequery plugin to keep cart alive获取 .live 或 .delegate 或 livequery 插件以保持购物车存活
【发布时间】:2012-09-30 00:21:30
【问题描述】:

我有这个加载 ajax 的#container,我正试图让它与我的一些插件一起玩得很好。到目前为止,我设法让 scrollTo 和一个灯箱使用 jquery.live 在这个“死亡容器”内工作,但我喜欢的“添加到购物车”按钮没有运气。几天来,我一直在玩 .delegate、livequery 插件等,但我真的不够先进,无法弄清楚什么地方去哪里了。 (我对自己在做什么的理解很肤浅。)

这是我的购物车插件,它相当小而简单。您能否就应该在哪里插入什么(.live、.delegate 或 .livequery,或者可能完全是其他东西)提出建议?

(注意:shopme p = 添加到购物车按钮,需要将其插入加载 ajax 的“死亡容器”中。购物车的其余部分存在于所述容器之外并且工作正常,因为它没有被 ajax 插入.)

$(document).ready(function(){

$('.wooo').bloooming_shop();

$('body').append('<div id="panel"><div id="panelcontent"></div><div class="panelbutton" id="hidepanel" style="display: none;"><a><font class="cartfont2">hide cart</font></a></div></div><div id="showpanel" class="panelbutton" style="display: visible;"><a><font class="cartfont">shopping cart</font></a></div><div id="btntarget"></div>');
$('#panelcontent').hide();

$.ajax({
    type: "GET",
    url: "/wooo/cart.php",
    async: false,
    dataType: "html",
    success: function(html){
        $('#panelcontent').html(html);
    }
});



$(".panelbutton").click(function(){
    $("#panel").animate({
        height: "200px"
    }, "fast",function(){
        $('#panelcontent').show();
    });
    $("#hidepanel").fadeIn();
    $("#showpanel").fadeOut();

}); 

  $("#hidepanel").click(function(){
    $("#panel").animate({
        height: "0px"
    }, "fast", function(){ 
        $("#showpanel").fadeIn();
        $('#panelcontent').hide();
    });

    $("#hidepanel").fadeOut();
   });  


   // START 'ADD TO CART' BUTTONS

$('.shopme p').click(function(){

    var pid = $(this).attr('rel');

    $('body').prepend('<div class="shadow" id="'+$(this).attr('rel')+'_shadow"></div>');

    var shadow = $('#'+pid+'_shadow');

        shadow.width($(this).parent().css('width')).height($(this).parent().css('height')).css('top', $(this).parent().offset().top).css('left', $(this).parent().offset().left).css('opacity', 0.5).show();
     shadow.css('position', 'absolute');

     shadow.animate( {
            width: $('#btntarget').innerWidth(), 
            height: $('#btntarget').innerHeight(), 
            top: $('#btntarget').offset().top, 
            left: $('#btntarget').offset().left 
            }, { 
            duration: 2000 
            } )
        .animate({ 
            opacity: 0 
        },
        { 
        duration: 700,
        complete: function(){

        shadow.remove();

    }

    });

    var option = $('#'+pid+' .woooptions').val();

    var formData = 'pid=' + pid + '&option=' + option; 

    $.ajax({
        type : 'POST',
        url : '/wooo/cart.php',
        data : formData,
        success : function (html) {
            $('#panelcontent').html(html);
        }
    });


}); 


$('.removeitem').live('click', function() { // .LIVE is used here   
    rid = $(this).attr('id');
    rop = $(this).attr('rel');

    var remData = 'remove=' + rid + '&rop=' + rop; 

    $.ajax({
        type : 'POST',
        url : '/wooo/cart.php',
        data : remData,
        success : function (html) {
            $('#panelcontent').html(html);
        //  alert('thx');
        }
    });

});




}); // document

function checkOut(){
        jQuery.ajax({
          url: "/wooo/cart.php",
          type: "POST",
        data : "destroysession=true",
          success: function(data, textStatus, jqXHR){
            if(data){
                window.location.href=jQuery('a.checkout').attr("data-href");
            }else{
                console.log("There is no data!")
            }
          },
          error: function(jqXHR, textStatus, errorThrown){
            console.log("AJAX ERROR: "+errorThrown)
          }
        });
}

/** replace ******/

jQuery.fn.bloooming_shop = function(){

    this.each(function(){

        var elem = $(this);

        var cl = 'bt1';
        var id = $(this).html();
        var opt = $(this).attr('options');
        var text = $(this).attr('text');
        var price = $(this).attr('price');
    //  alert(price);

        if (text == undefined) {
            text = 'add to cart';
        }

        if (opt == 'true' && price != 'true' ) {
            cl = 'bt3';
        }

        if (price == 'true' && opt == 'true') {
            cl = 'bt4';
        }

        if (price == 'true' && opt != 'true') {
            cl = 'bt2';
        }

        elem.removeClass('wooo');
        elem.addClass('shopme');
        elem.addClass(cl);
        elem.attr('id','pid'+id);
        elem.html('<p rel="pid'+id+'" class="'+cl+'">'+ text +'</p>');

        // get product data
        if (price == 'true' || opt == 'true') {

            $.ajax({
                type : 'GET',
                url : '/wooo/functions.php?mode=p_data&id='+id+'&opt='+opt+'&price='+price,
                success : function (html) {

                    elem.append(html);

                    if (jQuery().sSelect) {
                            elem.children('.woooptions').sSelect();
                     } 

                    // change price
                    $('.woooptions').change(function(){
                        var selid = $(this).attr('id');
                        var rel = $('#'+selid+' option:selected').attr('rel');

                        if (rel != undefined) {
                              $(this).parent().children('.woooprice').html(rel);
                        }
                    });

                }
            });
        }

    });

return false;

};

如何让这个插件保持活力,即使在 ajax'ed-in #container 中?我真的只需要将“添加到购物车”按钮(shopme p)放在所述容器 div 中。谢谢。

【问题讨论】:

  • 首先,把$.live()抛在脑后;它效率低下且“已被长期弃用”。 $.delegate() 也是,我相信。如果您需要挂钩某个元素的事件,请使用 $.on()
  • .on() 方法将事件处理程序附加到 jQuery 对象中当前选定的元素集。从 jQuery 1.7 开始,.on() 方法提供了附加事件处理程序所需的所有功能。有关从旧 jQuery 事件方法转换的帮助,请参阅 .bind().delegate().live() - jQuery API 我突出显示的列表已弃用或有计划这样做。
  • .on 不适用于只有 live 有效的所有方法。弃用与否没有其他办法。是的,他们希望您将其从大多数事情中删除,因为在... 90%的情况下工作,但是仍然有很多时候只有现场才能完成这项工作,是的,它很昂贵,但如果这是您必须的做。
  • @Iscariot - 嘿,我明白了;我花了一段时间才“得到”$.on()。在我使用它的前几次,我什至无法让它工作。上面的引用来自 jQuery API;这不是我编的。我想不出上次我想使用$.live() 是什么时候,我想出了$.on()

标签: php jquery


【解决方案1】:
.live("click", function(){

而不仅仅是点击。

【讨论】:

  • 不不不,$.live() is deprecated。在这里你是老虎。
  • 在某些情况下,这是做某事的唯一方法。如果您正在引入通过 Ajax 加载的新内容,那么您必须重新加载 dom 才能访问新内容。我可能是错的,如果我错了,我很想知道怎么做,我会确保从现在开始就是这样。
  • $.live() 是一种反应式的事件处理方式; $.on() 是一种主动、更有条理的事件处理方法。 $.live() 将所有内容委托给 document,这是非常低效的。 $.on() 说“事先确定会发生什么并适当地准备接受它”。 $.live() 杂乱无章。 $.on() 更难,但更胜一筹。
  • 我同意,但这仍然不能解决我提出的问题。 On 是 100% 更好的功能,但是我从来没有能够做我之前提到的 on。
  • 我不确定你的意思;你有那个用例的演示吗?
猜你喜欢
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-10
  • 1970-01-01
相关资源
最近更新 更多