简体   繁体   English

传递给函数时,JavaScript变量不保持期望值

[英]JavaScript variable not holding expected value when passed into function

I'm using some jQuery to let users manipulate images. 我正在使用一些jQuery让用户操纵图像。 The user should be able to double-click an image to select it, then click on some buttons which appear below for resizing or flipping the image. 用户应该能够双击图像以将其选中,然后单击下面显示的某些按钮以调整图像大小或翻转图像。

The image gets passed to the image_editor function like this: 图像传递给image_editor函数,如下所示:

$('section img').dblclick(function () {
    item_editor(this);
});

Then the item_editor function looks like this: 然后item_editor函数如下所示:

var item_editor = function (activeItem) {

    var $activeItem = $(activeItem);

    // Show border around current activeItem
    $('.activeItem').removeClass('activeItem');
    $activeItem.addClass('activeItem');

    // Flip the selected image when the button's clicked
    $('div#flip').click(function () {
        $activeItem.toggleClass('flipped');
    });

}

I put this up into a jsFiddle here: http://jsfiddle.net/sarahg/6sE5F/30/ 我在这里把它放到一个jsFiddle: http//jsfiddle.net/sarahg/6sE5F/30/

The problem I'm having here is that the Flip button works the first time you use it, but if you select a different image and try to flip that one, everything which had already been flipped flips again. 我在这里遇到的问题是翻转按钮在你第一次使用时工作,但是如果你选择一个不同的图像并试图翻转那个图像,那么已翻过的所有图像都会翻转。 The activeItem variable is not what I'd imagine it should be. activeItem变量不是我想象的那样。

I did some searching around and I think this has to do with JavaScript closures, but I haven't been able to understand them enough to make my code work. 我做了一些搜索,我认为这与JavaScript闭包有关,但我无法理解它们以使我的代码工作。

jQuery event listeners don't get replaced. jQuery事件监听器不会被替换。 They just get stacked on top of one another and all execute when they catch your click event, which is what happens in your case. 它们只是堆叠在一起,当它们捕获你的click事件时都会执行,这就是你的情况。 If you double click on an image an even number of times, it gets flipped an even number of twice, so it looks like nothing happened to it. 如果你偶然双击一个图像,它会被翻转偶数两次,所以它看起来没有任何反复意义。

Add the click logic outside of the event handler: 在事件处理程序之外添加单击逻辑:

$('section img').dblclick(function() {
    item_editor(this);
});

var item_editor = function(activeItem) {
    $('.activeItem').removeClass('activeItem');
    $(activeItem).addClass('activeItem');
}

$('div#flip').click(function () {
    $('.activeItem').toggleClass('flipped');
});

Demo: http://jsfiddle.net/6sE5F/33/ 演示: http//jsfiddle.net/6sE5F/33/

You need to unbind the previous "click" handler before binding a new one http://jsfiddle.net/6sE5F/31/ 在绑定一个新的http://jsfiddle.net/6sE5F/31/之前,您需要取消绑定之前的“click”处理程序

  $('div#flip').unbind('click').click(function () {
    // ...

Successive requests to jQuery that it bind a handler for a particular event simply add more handlers. 对jQuery的连续请求,它绑定特定事件的处理程序,只需添加更多处理程序。 The old ones don't go away unless you make them go away. 除非你让它们消失,否则它们不会消失。

In case there might be other "click" handlers bound to that element, you can use a qualifier to tell jQuery that you're only talking about a particular kind of "click" handler: 如果可能有其他“click”处理程序绑定到该元素,您可以使用限定符告诉jQuery您只是在谈论特定类型的“click”处理程序:

  $('#flip').unbind('click.kitten-flipper').on('click.kitten-flipper', function() {
    // ...

The ".something" qualifier doesn't affect the event handling process, but it identifies these click handlers as being related. “.something”限定符不会影响事件处理过程,但会将这些点击处理程序标识为相关。 Other click handlers bound without a qualifier or with different qualifiers won't be affected by the call to `.unbind'. 其他没有限定符或不同限定符的单击处理程序不会受到对`.unbind'的调用的影响。

You should not nesting click handler: 你不应该嵌套点击处理程序:

DEMO DEMO

$('section img').dblclick(function () {
    item_editor(this);
});

var item_editor = function (activeItem) {

    var $activeItem = $(activeItem);

    // Show border around current activeItem
    $('.activeItem').removeClass('activeItem');
    $activeItem.addClass('activeItem');

    // Flip the selected image when the button's clicked


}

 $('div#flip').click(function () {
       $('.activeItem').toggleClass('flipped');
    });

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

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