jQuery toggle()函数.我想对更好的实践提出一些批评或建议

jQuery toggle() function. I would like some critique or suggestions for better practice

本文关键字:函数 toggle 更好 jQuery      更新时间:2023-09-26

我的代码正在按我希望的方式工作,但我正在寻找任何关于如何修剪或修改它以提高效率的评论:

$('.toggle-comments').hide(); // this hides the results div when the page loads.                   
$(".comments-toggle-click").click(function () {                             
  $(".toggle-comments").slideToggle("slow");
  $(".comments-toggle-click").remove();
});             

与其在页面加载时调用$('.toggle-comments').hide();,为什么不使用display:none;呈现注释呢?

CSS:

.toggle-comments {
  display: none;
}

JavaScript:

$('.comments-toggle-click').click(function () {                             
  $('.toggle-comments').slideToggle('slow', function () {
    $('.comments-toggle-click').remove()
  })
})

如果您想延迟删除链接直到动画完成,那么诀窍是向.slideToggle传递一个回调函数。这可能是你在问题中回避的问题,也可能不是。