简体   繁体   English

帮助您理解如何在运行中创建的新dom元素上执行javascript背后的逻辑

[英]help with understanding the logic behind how javascript executes on new dom elements being created on the fly

I have a this set up 我有一个这样的设置

<ul>
<li>item 1</li>
</ul>

my js let me make them sortable and what not, one feature I have is that via ajax i can create a new list item on the fly. 我的js让我可以对它们进行排序,而没有,我的一个功能是可以通过ajax即时创建一个新的列表项。 But whats interesting is that I have a hoverIntent letting me hover over the list item and a delete and edit icon appears letting me perform the intended actions. 但是有趣的是,我有一个hoverIntent可以将我悬停在列表项上,并且出现一个删除和编辑图标可以让我执行预期的操作。

My problem is that when I make a new list item on the fly the hoverIntent isn't applied to it. 我的问题是,当我即时创建一个新列表项时,hoverIntent并未应用到它。 The code created is identical to the other list items, I know it works because when I refresh the page and reload the js then the hoverIntent kicks in and applies the hover effects to the new items created. 创建的代码与其他列表项相同,我知道它是有效的,因为当我刷新页面并重新加载js时,hoverIntent就会起作用,并将悬停效果应用于所创建的新项目。

None of this works on the fly tho, how can I make this work? 这些都不是即时运行的,我该如何做呢?

my js for creating a new list item is: 我的用于创建新列表项的js是:

var timestamp = 0;
    $('.new-group').click(function(e){

        // Only one group per 5 seconds is allowed:
        if((new Date()).getTime() - timestamp < 5000) return false;

        $.get("resources/ajax/groups.ajax.php",{
            action :'new',
            uid     : uid,
            text   :'New Group, Doubleclick to Edit.'
        },function(data){
            $(data).hide().appendTo('.grouplist').fadeIn();
            //GROUP.find(".groups-action-div").show('slow');
        });
        timestamp = (new Date()).getTime();
        e.preventDefault();
    });

I'm betting that in my success function is where I can make this work, 我敢打赌,在我的成功职能中,我可以完成这项工作,

you can log in and see my problem at www.helixagent.com with login as test/password go into the Contacts tabs and click on the + icon to make a new group 您可以登录并在www.helixagent.com上查看我的问题,并以test / password身份登录进入“联系人”选项卡,然后单击“ +”图标以创建一个新群组

hoverIntent loads the effect on page load, meaning that when you create a new element, you've already executed the hoverIntent aspects, and it doesn't apply to the newly create element. hoverIntent会在页面加载时加载效果,这意味着在创建新元素时,您已经执行了hoverIntent方面,并且不适用于新创建的元素。

Basically, you just have to run hoverIntent again 基本上,您只需要再次运行hoverIntent

I haven't tested it, but you'd need to do something like this: 我尚未测试过,但是您需要执行以下操作:

var timestamp = 0;
$('.new-group').click(function(e){

    // Only one group per 5 seconds is allowed:
    if((new Date()).getTime() - timestamp < 5000) return false;

    $.get("resources/ajax/groups.ajax.php",{
        action :'new',
        uid     : uid,
        text   :'New Group, Doubleclick to Edit.'
    },function(data){
        $(data).hide().appendTo('.grouplist').fadeIn();
        //GROUP.find(".groups-action-div").show('slow');
    });
    timestamp = (new Date()).getTime();
    e.preventDefault();


    var config_names = {
        over: show_delete_names,
        timeout: 200,
        out: hide_delete_names
    };
    $(".contactlist li:last-child").hoverIntent( config_names );
});

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

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