我在這段代碼中遇到了一些麻煩,我想做一個判斷,如果next().children()
div沒有長度,那麼做一個ajax要求。但它始終使ajax甚至要求div有內容。我的jQuery代碼有些麻煩
下面是HTML代碼:
<noscript>
<style>
.more{display:none;}
</style>
</noscript>
<!-- main content -->
<p class="click hidden" rel="12345">View more</p>
<div class="more">
<h5>Relative post</h5>
<div class="relative-post"></div>
<!-- comment area -->
</div>
<!-- main content -->
<p class="click hidden" rel="23456">View more</p>
<div class="more">
<h5>Relative post</h5>
<div class="relative-post"></div>
<!-- comment area -->
</div>
這裏是jQuery代碼:
jQuery(document).ready(function(){
$(".click").live('click',function() {
if($(this).next('.more').is(':visible')){
$(this).next('.more').css('display','none');
}else{
$(this).next('.more').css('display','block');
}
if($(this).next('.more').children('.relative-post:has(*)').length){
}else{
var $this = $(this);
var item_id = $this.attr('rel');
$.ajax({
url: "process",
dataType: "html",
type: 'POST',
data: 'post=' + item_id,
cache: false,
success: function(data){
$this.next('.more').children('.relative-post').html(data);
}
});
}
}
});
});
可以清理你的顯示/隱藏代碼 – BZink
Btw live()方法已棄用。在()上使用。 – Ignas