我的jquery代码有一些问题
我在这段代码中遇到了一些问题,我想判断我的jquery代码有一些问题,jquery,Jquery,我在这段代码中遇到了一些问题,我想判断next().children()div是否没有变长,然后执行ajax命令。但是,即使div有内容,ajax也总是需要 以下是html代码: <noscript> <style> .more{display:none;} </style> </noscript> <!-- main content --> <p class="click hidden" rel="12345">View
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>
试一试
if($(this).next('.more').children('.relative post').html().length){
替换代码
if($(this).next('.more').children('.relative-post:has(*)').length)
有以下几点
if($(this).next('.more').children('.relative-post').html())
您可以使用jQuery toggle()清理显示/隐藏代码……顺便说一句,不推荐使用live()方法。请在()上使用。您可能想使用
内容,而不是子项。我还是投了赞成票。内容也会得到textnodes,但我不知道在这里如何使用它,因为:empty
表达式会检查元素是否为空,这也包括textnodes?
if($(this).next('.more').children('.relative-post').html())
$(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);
}
});
}
});
});