jQuery中是否有onAvailable()函数? [英] Is there a onAvailable() function in jQuery?

查看:154
本文介绍了jQuery中是否有onAvailable()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将新的html插入DOM之后,我需要向其添加一些侦听器.

After inserting new html to DOM,I need to add some listeners to it.

但是新元素无法立即使用.

But the new elements are not available at once.

我的代码:

$('#loader').clone().removeAttr('id').load("Views/chatBox.html").appendTo('body');
$('#chat')
    .focus(function() {
        $(this).addClass('jV');
    })
    .blur(function() {
        $('#chat').removeClass('jV');
    });

哪个不起作用.

使用live()仍然无法正常工作:

Using live() still not working:

$('#chat').live('focus',function() {
    $('#chat').addClass('jV');
})
.live('blur',function() {
    $('#chat').removeClass('jV');
});

推荐答案

如果#chat在要加载的chatBox.html中,则可以利用已加载的回调:

If #chat is in the chatBox.html that you are loading then you can take advantage of the callback that load has:

$('#loader').clone().removeAttr('id').load("Views/chatBox.html",function(){
    $('#chat').focus(function() {
        $(this).addClass('jV');
    })
    .blur(function() {
        $('#chat').removeClass('jV');
    });
}).appendTo('body');

http://docs.jquery.com/Ajax/load#urldatacallback

这篇关于jQuery中是否有onAvailable()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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