jQuery空()回调没有被解雇 [英] jQuery empty() callback not being fired

查看:81
本文介绍了jQuery空()回调没有被解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这个 empty()函数不会触发。我错过了这个类/回调问题?

  $('。commentsButton')。click(function(e){ 
e.preventDefault();
if('.openComments')。length){
$(this).closest('。box')。removeClass(openComments); ()。

$('。commentsBox')。empty(function(){
$(this).closest('.box')。find('。commentsBox')。load url.com');
$(this).closest('.box').addClass(openComments);
});
} else {
$(this).closest('。box')。find('。commentsBox')。load('url.com');
$(this).closest('。box')。addClass(openComments);
}
});

< div class =box>
< a class =commentsButtonhref =#>< / a>
< div class =commentsBox>< / div>
< / div>
< div class =box>
< a class =commentsButtonhref =#>< / a>
< div class =commentsBox>< / div>
< / div>


解决方案

  $( '''。''。'。'。'。'。'。'。'on'('click',function(e){
e.preventDefault();

$('。box')。removeClass('openComments');
$('。commentsBox')。empty();
$(this).closest('。box')。addClass('openComments')
.find('。commentsBox' ).load('url.com');
});


I'm emptying an element of all children, then applying an element with the same class but within separate element.

The callback on this empty() function isn't firing. Am I missing something with this class/callback issue?

$('.commentsButton').click(function(e){
e.preventDefault();
if ('.openComments').length) {
    $(this).closest('.box').removeClass(openComments);

    $('.commentsBox').empty(function(){
            $(this).closest('.box').find('.commentsBox').load('url.com');
            $(this).closest('.box').addClass(openComments);
        });
} else {
        $(this).closest('.box').find('.commentsBox').load('url.com');
        $(this).closest('.box').addClass(openComments);
    }
});

<div class="box">
  <a class="commentsButton" href="#"></a>
   <div class="commentsBox"></div>
</div>
<div class="box">
  <a class="commentsButton" href="#"></a>
   <div class="commentsBox"></div>
</div>

解决方案

$('.commentsButton').on('click', function(e){
    e.preventDefault();

    $('.box').removeClass('openComments');
    $('.commentsBox').empty();
    $(this).closest('.box').addClass('openComments')
           .find('.commentsBox').load('url.com');
});

这篇关于jQuery空()回调没有被解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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