JQuery对每个

JQuery对每个,jquery,each,conditional-statements,Jquery,Each,Conditional Statements,我有这个JQuery函数: $(document).ready(function() { $("div.descript_long").each(function() { var descript_length = $("div.descript_long").text().length; if(descript_length < 160) $(this).next().("a.toggle_more").attr('style'

我有这个JQuery函数:

$(document).ready(function() {
    $("div.descript_long").each(function() { 
        var descript_length = $("div.descript_long").text().length;

        if(descript_length < 160)
        $(this).next().("a.toggle_more").attr('style','display:none');
    else
        $(this).next().("a.toggle_more").removeAttr("style");

    });
});
$(文档).ready(函数(){
$(“div.descrippt_long”)。每个(函数(){
变量descrippt_length=$(“div.descrippt_long”).text().length;
如果(描述长度<160)
$(this).next()(“a.toggle_more”).attr('style','display:none');
其他的
$(this).next()(“a.toggle_more”).removeAttr(“style”);
});
});
此页面有多个div,其类descript_long。我希望此函数遍历每个函数,如果div中的文本长度小于160,我希望通过将样式设置为display:none来隐藏下一个带有类toggle_more的函数。否则我不想发生任何事情

这是一个示例div(其中有几个示例):


测试
测试

所以我的问题是它什么都没做。我希望这是个语法错误。但也许我写错了函数,它没有表达我真正想要它做什么。另一组眼睛检查这一点将非常感谢。

我认为您的代码实际上并不是在寻找下一个a元素,因为a元素位于找到的div中,因此可以使用
.find()
。然后您应该使用
.hide()
.show()
而不是自己直接应用样式,或者更好地添加和删除您定义的css类,该类将为元素设置
display:none

$(document).ready(function() {
    $("div.descript_long").each(function() { 
        var descript_length = $("div.descript_long").text().length;

        if(descript_length < 160)
        $(this).find("a.toggle_more").hide();
    else
        $(this).find("a.toggle_more").show();

    });
});
$(文档).ready(函数(){
$(“div.descrippt_long”)。每个(函数(){
变量descrippt_length=$(“div.descrippt_long”).text().length;
如果(描述长度<160)
$(this.find(“a.toggle_more”).hide();
其他的
$(this.find(“a.toggle_more”).show();
});
});
您的
.next()
调用已中断。这是为了测试兄弟节点,而不是子节点

尝试:

$(文档).ready(函数(){
$(“div.descrippt_long”)。每个(函数(){
变量descrippt_length=$(this).text().length;
如果(描述长度<160){
$(this).find(“a.toggle_more”).first().hide();
}否则{
$(this).find(“a.toggle_more”).first().show();
}
});
});
EDIT最初是
.nextAll()
而不是
find
,因为我误读了OP的标记。

尝试对文本使用“this”并使用find

$(document).ready(function() {     
    $("div.descript_long").each(function() {          
        var descript_length = $(this).text().length;
        if(descript_length < 160)         
           $(this).next().find("a.toggle_more").attr('style','display:none');     
        else         
           $(this).next().find("a.toggle_more").removeAttr("style");     
    }); 
});
$(文档).ready(函数(){
$(“div.descrippt_long”)。每个(函数(){
变量descrippt_length=$(this).text().length;
如果(描述长度<160)
$(this).next().find(“a.toggle_more”).attr('style','display:none');
其他的
$(this).next().find(“a.toggle_more”).removeAttr(“style”);
}); 
});
编辑以添加查找。

试试这个

$(document).ready(function() {
    $("div.descript_long").each(function() { 
       $(this).nextAll("a.toggle_more").css({display: ($(this).text().length < 160)?"none":"block"});
    });
});
$(文档).ready(函数(){
$(“div.descrippt_long”)。每个(函数(){
$(this.nextAll(“a.toggle_more”).css({display:($(this.text().length<160)?“none”:“block”});
});
});

这是无效的:
$(This).next()(“a.toggle\u more”)
是的,我忽略了这个发现。修正了你发布的评论。谢谢你的建议,但我一直无法让它发挥作用。我也在JSFiddle@kramden88中测试了它,是的,因为您唯一的
.descript\u long
div中没有
。toggle\u more
链接!试试这个:
$(document).ready(function() {     
    $("div.descript_long").each(function() {          
        var descript_length = $(this).text().length;
        if(descript_length < 160)         
           $(this).next().find("a.toggle_more").attr('style','display:none');     
        else         
           $(this).next().find("a.toggle_more").removeAttr("style");     
    }); 
});
$(document).ready(function() {
    $("div.descript_long").each(function() { 
       $(this).nextAll("a.toggle_more").css({display: ($(this).text().length < 160)?"none":"block"});
    });
});