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

时间:2012-01-22 23:05:16

标签: javascript jquery user-interface toggle

我的代码正在按照我的意愿运行,但我正在寻找关于如何修剪或修改以提高效率的任何批评:

$('.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();
});             

2 个答案:

答案 0 :(得分:2)

而不是在页面加载时调用$('.toggle-comments').hide();,而不是使用display:none;呈现评论?

答案 1 :(得分:1)

CSS:

.toggle-comments {
  display: none;
}

JavaScript的:

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

如果要在动画完成之前延迟删除链接,诀窍是传递.slideToggle回调函数。这可能是您在问题中可能遇到的问题,也可能不是。