我在这段代码中遇到了一些问题,如果next().children()
div没有长度,我想做一个判断,然后做一个ajax要求。但是,即使div有内容,它总是要求ajax。
这是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);
}
});
}
}
});
});
答案 0 :(得分:2)
$(function() {
$(document).on('click', '.click', function() {
var next = $(this).next('.more');
next.toggle(!next.is(':visible'));
if (next.children('.relative-post:empty').length) {
var item_id = $(this).attr('rel');
$.ajax({
url: "process",
dataType: "html",
type: 'POST',
data: {post: item_id},
cache: false,
success: function(data) {
next.children('.relative-post').html(data);
}
});
}
});
});
答案 1 :(得分:0)
尝试
if($(this).next('.more').children('.relative-post').html().length){
答案 2 :(得分:0)
替换代码
if($(this).next('.more').children('.relative-post:has(*)').length)
以下
if($(this).next('.more').children('.relative-post').html())