slidetoggle空div应该不起作用

时间:2017-11-15 07:49:21

标签: javascript slidetoggle



$('.cat').click(function(){
    if ($(this).next().text() == '') {
        return false;
    }

    $(this).next().slideToggle();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='themea cat'>lorem ipsum</div>
<div class='titlewrap'>
    <ul></ul>
</div>
<div class='themea cat'>lorem ipsum</div>
&#13;
&#13;
&#13;

nextcattitlewrap且没有文字。不过,点击cat正在尝试slideToggle

1 个答案:

答案 0 :(得分:1)

您需要修剪字符串以避免空白。

$('.cat').click(function() {
  if ($(this).next().text().trim() == '') {
    return false;
  }
  $(this).next().slideToggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='themea cat'>lorem ipsum</div>
<div class='titlewrap'>
  <ul>
  </ul>
</div>

<div class='themea cat'>lorem ipsum</div>