jQuery的触发代表团尽快建立代表团、jQuery

由网友(怀里的猫)分享简介:我试图使一个自动消失通知框,通知框装槽AJAX,所以我不能使用 $(文件)。就绪(); 。 我发现了我使用广泛的漂亮 .delegate()的功能,所以基本上现在AJAX创建一个新的< D​​IV CLASS = 变脸>愚蠢的猴子和LT; / DIV> ,我想它基于类定义2秒后消失,这是我有这么远:...

我试图使一个自动消失通知框,通知框装槽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(#信息)触发(事件类型);
 

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

jQuery输入框创建标签代码

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.