简体   繁体   English

针对所有班级,仅选择单击的元素

[英]Targeting all of class, select only clicked element

I posted a question yesterday and then deleted it, thinking I had figured that there was nothing wrong with this part of my code. 我昨天发布了一个问题,然后将其删除,以为我认为我的代码的这一部分没有问题。 However, after testing there still seems to be an issue. 但是,经过测试似乎仍然存在问题。

Within the code below I want $(this) to select only the clicked element, not all of the class that I am targeting. 在下面的代码中,我希望$(this) 选择clicked元素,而不是我定位的所有类。 However, the console logs 2, 3, 4, etc... not 1! 但是,控制台记录的2、3、4等...不是1!

Basically I need the class "action" to be added only to the clicked element. 基本上,我只需要将class“ action”添加到clicked元素中。 Appreciate any help! 感谢任何帮助!

$("body").on("mousedown", ".moveable", function(e){
    var clickX = mouseX;
    var clickY = mouseY;

    $(this).addClass("action");
    console.log($(".action").length);

    inX = clickX - $(".action").position().left;
    inY = clickY - $(".action").position().top;

    timeout = setInterval(function(){

        $(".action").css("left", clickX + (mouseX - clickX) - inX);
        $(".action").css("top", clickY + (mouseY - clickY) - inY);

    }, 10);

    return false; 

});

I know I could use $(".moveable").on("mousedown", function() {...}); 我知道我可以使用$(".moveable").on("mousedown", function() {...}); , $(this) then targets just the clicked element with the "moveable" class but I need the event to fire on dynamically added elements, hence me binding the event in the way I am. ,然后$(this)仅以“ moveable”类定位被点击的元素,但是我需要在动态添加的元素上触发事件,因此我以自己的方式绑定了该事件。 Maybe the way I am doing this is the problem? 也许我这样做的方式是问题吗?

I hope you can help! 希望您能提供帮助!

Mike 麦克风

I think what you need is to remove the action class from other elements as below. 我认为您需要从其他元素中删除操作类,如下所示。 Also note to cache the jQuery selector so that you don't have to run the selector all the time. 还要注意缓存jQuery选择器,这样您就不必一直运行选择器。

If you are removing the action class, you will need to remove those element's interval also. 如果要删除操作类,则还需要删除这些元素的间隔。

$("body").on("mousedown", ".moveable", function (e) {
    var clickX = mouseX;
    var clickY = mouseY;

    $('.action').removeClass('action').each(function () {
        //clear other element's interval also
        clearInterval($(this).data('moveable-timer'));
    });
    var $action = $(this).addClass("action");
    console.log($action.length);

    var position = $action.position();
    inX = clickX - position.left;
    inY = clickY - position.top;

    var timeout = setInterval(function () {

        $action.css("left", clickX + (mouseX - clickX) - inX);
        $action.css("top", clickY + (mouseY - clickY) - inY);

    }, 10);
    $action.data('moveable-timer', timeout);

    return false;

});

In your code you are always targeting .action class which is creating issue. 在您的代码中,您始终以正在制造问题的.action类为目标。 User $(this) and which will target only current element. 用户$(this)并且仅将当前元素作为目标。 Try this: 尝试这个:

$("body").on("mousedown", ".moveable", function(e){
    var clickX = mouseX;
    var clickY = mouseY;

    var $el = $(this);
    $el.addClass("action");

    inX = clickX - $el.position().left;
    inY = clickY - $el.position().top;

    timeout = setInterval(function(){

        $el.css("left", clickX + (mouseX - clickX) - inX);
        $el.css("top", clickY + (mouseY - clickY) - inY);

    }, 10);

    return false; 

});

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

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