简体   繁体   English

moused在mousedown上没有处理动态生成的元素

[英]Jquery on mousedown not working on dynamically generated elements

So i'm trying to create a js/css "wave game" like tower defense ones. 所以我正在尝试创建一个像塔防一样的js / css“wave游戏”。 When all the pre-generated enemys from first wave are dead, it spawns the second wave and so on. 当所有来自第一波的预先生成的敌人死亡时,它会产生第二波,依此类推。

So far so good. 到现在为止还挺好。 The problem is that i just can't attack mobs dynamically spawned within second wave. 问题是我无法攻击在第二波内动态生成的怪物。

I used to try .live() in similar cases, but its deprecated, so i'm trying .on(), as instructed 我曾经在类似的情况下尝试过.live(),但是它被弃用了,所以我正在尝试.on(),按照指示

$('.enemy').on('mousedown' , function(event) { $('。enemy')。on('mousedown',function(event){

//attack code //攻击代码

} }

Its working fine for initial mobs (1st wave) but it still just not working on dynamic mobs (>= 2nd wave) 它适用于最初的怪物(第一波),但它仍然只是没有动态的怪物(> =第二波)

Help, guys, please? 求救,伙计们好吗?

You need to specify an element that is already there when the DOM is created. 您需要在创建DOM时指定已存在的元素。 In the parameters, you specify the elements you want to add the mousedown method. 在参数中,指定要添加mousedown方法的元素。 By simply assigning $('.enemy') , it will attach the method to those that are already present in the DOM. 通过简单地分配$('.enemy') ,它会将方法附加到已经存在于DOM中的方法。

$('body').on('mousedown', '.enemy', function(event) {

    //attack code

}

As Wex mentioned in the comments, instead of writting $('body') you should use the container's name (the container which wraps the .enemy elements. This way, when a .enemy element is added, the event doesn't need to bubble all the way up to the body tag. 正如Wex在评论中提到的,你应该使用容器的名称(包装.enemy元素的容器$('body')而不是写$('body') 。这样,当添加.enemy元素时,事件不需要泡沫一直到body标签。

The binding '.on()' works only with the content that created earlier then the script ran. 绑定'.on()'仅适用于脚本运行之前创建的内容。 So one solution could be you bind the event to the parent element. 因此,一种解决方案是将事件绑定到父元素。

    $('.PARENT_ELEMENT').on('mousedown', '.enemy', function(event){
    // your code here
    }

That should do it. 应该这样做。

I made this google like drop down suggestions search box and I faced a problem similar to yours where there was suggestions disappearing before the re-direct happened. 我做了这个谷歌像下拉建议搜索框,我遇到了类似于你的问题,在重新直接发生之前有建议消失。 I overcame it by using and modifing vyx.ca answer: 我通过使用和修改vyx.ca来克服它:

var mousedownHappened = false;
var clicked_link;

$("#search-box").blur(function(e) {
    if (mousedownHappened)// cancel the blur event
    {
        mousedownHappened = false;
        window.location.href = clicked_link;
    } else {
        // no link was clicked just remove the suggestions box if exists
        if ($('#search-btn').next().hasClass('suggestions')) {
            $(".suggestions").remove();
        }
    }
});
//attaching the event to the document is better 
$(document).on('mousedown', '.suggestions a', function() {
    clicked_link= $(this).attr('href');
    mousedownHappened = true;
});

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

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