简体   繁体   English

附加到dom元素的jQuery click事件处理程序

[英]jQuery click event handler attached to dom element

I am having hard time while building e-commerce cart module with jquery. 我在用jquery构建电子商务购物车模块时遇到了困难。 Lets say that if i write a tags in html like this: 可以说,如果我在html中这样写一个标签:

<div class="add-to-cart">+</div>

and then target it in my app: 然后将其定位到我的应用中:

this.$products,
this.$pa,
this.$ip,

this.$products = $('.shopperProducts'),
this.$pa = this.$products.find('.shopperAdd');

var self = this;

this.$ip = function() {
    var init = function(action, product) {
    /.../
    };

    self.$pa.on('click', function(event) {
        event.preventDefault();
        init('add', this);
    });
};

This method is possible while im displaying products because they are displayed by php on page refresh so i have all the + links generated by php on html. 我在显示产品时可以使用此方法,因为它们是在刷新页面时由php显示的,因此我拥有html上php生成的所有+链接。

The problem is on the checkout file, this is the page when i display entire cart filled with products, cart must be generated and handled in jQuery and AJAX. 问题出在结帐文件上,这是我显示装有产品的整个购物车时的页面,必须在jQuery和AJAX中生成和处理购物车。

And code that i showed you doesnt work in cart page beacuse those links are appended for each product via jQuery into the DOM. 我向您展示的代码在购物车页面中不起作用,因为这些链接通过jQuery通过每种产品附加到了DOM中。

I have been study possible methods and there are few, the most in favour is to do this: 我已经研究了可能的方法,但很少有人赞成这样做:

$(document).on('click', self.$pa, function(event) {

The problem with that solution is that it also is considered practice to be avoided due to high resources drain, i can see the difference in execution time myselfe, it takes a lot longer on low end devices. 该解决方案的问题在于,由于消耗大量资源,也应考虑避免这种做法,我自己可以看到执行时间的差异,在低端设备上花费的时间要长得多。 Is there some neat trick that can be used or method that is considered good practice to do in that situation? 在这种情况下,是否可以使用一些巧妙的技巧或被认为是好的做法?

<--- EDIT (Solution) ---> <---编辑(解决方案)--->

Instead of calling: 而不是调用:

this.$products = $('.shopperProducts'),
this.$pa = this.$products.find('.shopperAdd');

on the beginning, i have to call it after i load elements into DOM and then they became targetable, then i just have to use self.$ip(); 首先,我必须在将元素加载到DOM之后调用它,然后它们变为可定位的,然后我只需要使用self.$ip(); and event handlers can be attached. 并可以附加事件处理程序。 Without using any sort of workarounds, the solution was just to change order of executing commands. 在不使用任何解决方法的情况下,解决方案只是更改执行命令的顺序。

There are two main strategies that you can use for adding click handlers for elements that you dynamically add to the dom. 您可以使用两种主要策略为动态添加到dom的元素添加单击处理程序。

One, You can add click handlers to the DOM element each time you create one 一,您可以在每次创建一个时将点击处理程序添加到DOM元素中

var addToCartButton = $('<div class="add-to-cart">+</div>');
addToCartButton.on('click', function(){
  init('add', this);
};

// then you add your DOM element to the page
$('.container').append(addToCartButton);

Two, you can have a master click event listener on the page listen for all clicks where your buttons fall, and in your click handler, figure out whether the user is clicking on your element or not. 第二,您可以在页面上拥有一个主单击事件侦听器,以监听按钮落下的所有单击,然后在单击处理程序中确定用户是否在单击您的元素。 This is ultimately more efficient and you don't have to add or remove event handlers each time you add elements to your page. 最终这将更加高效,并且您不必在每次向页面添加元素时都添加或删除事件处理程序。 This pattern is called event delegation, and here's another post on Stack that probably explains it better than I can 这种模式称为事件委托,这是Stack上的另一篇文章,可能比我能更好地解释它。

What is DOM Event delegation? 什么是DOM事件委托?

$('.container').click(function(event){
  if ($(event.target).is('.add-to-cart') || $(event.target).parents().is('.add-to-cart')) {

    // handle add to cart
  }
})

BTW, your use of the self variable doesn't actually do anything, and neither does declaring this.$pa . 顺便说一句,您对self变量的使用实际上没有任何作用,也没有声明this.$pa You're basically accessing the property "$pa" of your this object, but not doing anything it. 基本上,您正在访问this对象的属性“ $ pa”,但不执行任何操作。

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

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