问题:
如果元素中没有 DOM,我正在尝试触发.click()
事件。<ul></ul>
这段代码背后的思考过程是,当用户点击x
item 时向左滑动,然后是.remove()
从 DOM 中。由于我精美的空白 HTML,它在ul
静止中留下了换行符和间距。因此,我无法让if()
语句正常运行。
即使通过使用$.trim()
然后比较它仍然不会触发.click()
事件。
HTML 的图片
HTML
<div class="btn-group pull-right nav-notice">
<span class="badge badge-info nav-notice-button">8</span>
<div class="notice-container">
<span class="notice-arrow"></span>
<ul class="notice-container-list">
<li><span class="label label-warning">Warning</span><span class="notice-message">Warning 12315125</span><i class="notice-remove icon-remove-circle"></i></li>
<li><span class="label label-success">success</span><span class="notice-message">Warning 12315125</span><i class="notice-remove icon-remove-circle"></i></li>
<li><span class="label label-important">important</span><span class="notice-message">Warning 12315125</span><i class="notice-remove icon-remove-circle"></i></li>
</ul>
</div>
</div>
jQuery
//live(), looking for past/present DOM
$('.notice-remove').live( "click", function(){
//no double dipping!
$this = $(this);
//Hide the x item, improves the animation a bit
$this.hide();
//animate the width of the li, to give it a slideoff effect
//when the animate completes, remove the element from the DOM
$this.parent('li').animate({
width: "0"
}, 'fast', '',
function(){
$(this).remove();
});
//get the html contents of the ul element
var str = $('.notice-container-list').html();
//trim the contents of the ul, and if they are exactly empty
//fire the click event to close the container
if( $.trim(str) === "" ){
log('Closing the Notice');
$('.nav-notice-button').click();
}
});