placeHolder id未使用style.display显示

placeHolder id not displaying using style.display

本文关键字:display 显示 style 未使用 id placeHolder      更新时间:2023-09-26

我不确定我的代码到底出了什么问题

<ul>
  <li>
    <p id='placeHolder' style='display: none;'>Empty NOW!</p>
    <ul class='list'>
       <li>Item One<a href='#' class='clear'>Remove</a></li>
       <li>Item Two<a href='#' class='clear'>Remove</a></li>
    </ul>
  </li>
</ul>
$('a.clear').click(function() { 
    $(this).parent().remove();
    if($('.list li').length == 0) { 
      document.getElementById('placeHolder').style.display='block';
      console.log('its empty now');
    }
    return false;
});

假设你从列表中删除了项目,而李现在为零,我不确定为什么placeHolder实际上没有显示placeHolder?

我使用console.log来确保长度确实经过了检查,并且它是一个有效的语句。关于getElementById为什么不启动,有什么建议吗?

为了验证条件$('.list li').length == 0,您必须删除两个列表项,或者通过找到一个公共选择器:

$('li a').click(function() { // the choice of the selector is of course up to you :)
    $(this).parent().remove();
    if($('.list li').length == 0) { 
      document.getElementById('placeHolder').style.display='block';
      console.log('its empty now');
    }
    return false;
});

或者通过移除父级的父级:$(this).parent().parent().remove();