简体   繁体   English

将单击绑定到jQuery中的两个元素时的奇怪行为

[英]Odd behavior when binding a click to two elements in jQuery

I have two images with the title "Show Options". 我有两张标题为“显示选项”的图像。 I want to assign a click event to both of them and I want the clicks to print different statements. 我想为他们两个都分配一个click事件,并且我希望这些单击可以打印不同的语句。

 console.log($('img[title*=\"Show\"]'));
  $('img[title*=\"Show\"]').each(function(index, value){
    switch(index){
        case 0:
            console.log('object');
            $(this).live('click', function(e) {
                console.log('object clicked');
            });
        break;

        case 1:
            console.log('record');
            $(this).live('click', function(e) {
                console.log('record clicked');
            });
        break;
    }
  });

ODD BEHAVIOR 奇怪的行为

  1. object and record are printed so I know there are 2 elements. objectrecord已打印,所以我知道有2个元素。
  2. When I click on the image that is associated with object, record is printed. 当我单击与对象关联的图像时, record被打印。
  3. When I click on the image that is associated with record, nothing is printed. 当我单击与记录关联的图像时,不会打印任何内容。

I am not sure how I can resolve this. 我不确定该如何解决。

The purpose of the .live method is to allow you to specify event handlers on DOM objects that may change, or do not yet exist. .live方法的目的是允许您在可能更改或不存在的DOM对象上指定事件处理程序。 This works because in fact no handler is attached at all. 之所以可行,是因为实际上根本没有附加处理程序。 Instead, a pre-existing handler at the root of the document looks through all of the selectors registered with .live() to determine if any of them is a match. 取而代之的是,位于文档根目录的预先存在的处理程序将浏览所有在.live()注册的选择器,以确定它们是否匹配。

In your example, you are passing in a DOM object directly, and not a jQuery selector. 在您的示例中,您直接传递DOM对象,而不是jQuery选择器。 So, what's probably happening (although, I'm not sure) is that it's trying to attach live events to selectors created by stringifying the DOM objects, which can lead to strange, unexpected results. 因此,可能正在发生的事情(尽管我不确定)是它试图将实时事件附加到通过对DOM对象进行字符串化创建的选择器上,这可能会导致奇怪的意外结果。

If you're trying to attach events to a single DOM object that will not change, just use the .bind() function. 如果您尝试将事件附加到不会更改的单个DOM对象,则只需使用.bind()函数。

If you really needed to use live, you could restructure the code so that you specify selectors that match the elements. 如果确实需要使用实时代码,则可以重组代码,以便指定与元素匹配的选择器。 For example: 例如:

var selector = 'img[title*=\"Show\"]';
$(selector).each(function(index, value){
    switch(index){
        case 0:
            console.log('object');
            $(selector+":eq(0)").live('click', function(e) {
                console.log('object clicked');
            });
        break;

        case 1:
            console.log('record');
            $(selector+":eq(1)").live('click', function(e) {
                console.log('record clicked');
            });
        break;
    }
});

In general, this is a very bad pattern, and there are much more eloquent ways to do things. 总的来说,这是一个非常糟糕的模式,并且有很多雄辩的方法来做事情。 However, it is in theory possible to make this pattern work. 但是,从理论上讲,可以使这种模式起作用。

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

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