jQuery的触发代表团尽快建立 [英] jQuery trigger delegation as soon as created

查看:109
本文介绍了jQuery的触发代表团尽快建立的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使一个自动消失通知框,通知框装槽AJAX,所以我不能使用 $(文件)。就绪();

我发现了我使用广泛的漂亮 .delegate()的功能,所以基本上现在AJAX创建一个新的< D​​IV CLASS = 变脸>愚蠢的猴子和LT; / DIV> ,我想它基于类定义2秒后消失,这是我有这么远:

  // AJAX请求处理
。jQuery的('#消息'),追加('< D​​IV CLASS =变脸>愚蠢的猴子<!/ DIV>');

jQuery的(身体)。代表('淡出',事件类型,函数()
{
    jQuery的(本).delay(2000).slideUp(500,函数()
    {
        jQuery的(这种)卸下摆臂();
    });
});
 

不管我替换 EVENTTYPE 带我不能引起我的处理程序之后创建的元素。我试过就绪负荷和各种替代品... 点击的作品,但不是我需要的,我不能触发点击上的匿名 DIV

解决方案

 的jQuery(身体)。代表('淡出',事件类型,函数()
{
    jQuery的(本).delay(2000).slideUp(500,函数()
    {
        jQuery的(这种)卸下摆臂();
    });
});

jQuery的('< D​​IV CLASS =变脸>愚蠢的猴子<!/ DIV>')。appendTo(#信息)触发(事件类型);
 

您可以链的优势。创建元素,将其附加到你的元素,然后调用触发新添加的格。

I'm trying to make an automatically disappearing notification box, the notification box is loaded trough ajax so I cannot use the $(document).ready();.

I found the nifty .delegate() function which I'm using extensively, so basically now ajax creates a new <div class="fade">Stupid monkey!</div> and I'd like it to disappear after 2 seconds based on the class definition, this is what I've got so far:

// ajax request handler
jQuery('#message').append('<div class="fade">Stupid monkey!</div>');

jQuery('body').delegate('.fade', eventType, function()
{
    jQuery(this).delay(2000).slideUp(500, function()
    {
        jQuery(this).remove();
    });
});

Whatever I replace eventType with I can't trigger my handler right after I create the element. I've tried ready, load and all sorts of alternatives... click works but isn't what I need and I can't trigger a click on the anonymous div.

解决方案

jQuery('body').delegate('.fade', eventType, function()
{
    jQuery(this).delay(2000).slideUp(500, function()
    {
        jQuery(this).remove();
    });
});

jQuery('<div class="fade">Stupid monkey!</div>').appendTo("#message").trigger(eventType);

You can take advantage of chaining. Create the element, append it to your element and then call trigger on the newly added div.

这篇关于jQuery的触发代表团尽快建立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆