我使用jQuery切换了我的博文。目前我可以同时打开所有帖子,但是我希望在点击一个帖子时关闭所有帖子。我需要添加到我的jQuery代码中才能执行此操作?
这是我的代码:
<script type="text/javascript">
$(document).ready(function() {
$('.toggle-section').hide();
});
</script>
<script type="text/javascript">
$(function() {
$('.entry-title').click(function() {
$(this).closest('.post').find('.toggle-section').slideToggle();
return false;
});
});
</script>
答案 0 :(得分:1)
$(function() {
$('.entry-title').click(function() {
var clicked = this; // take a reference of clicked element
// to use it within hide() callback scope
// hide all visible sections
$('.toggle-section:visible').hide(function() {
// show the clicked
$(clicked).closest('.post').find('.toggle-section').slideDown();
});
return false;
});
});
您应该将所有代码汇总为两部分:
<script type="text/javascript">
$(function() {
$('.toggle-section').hide(); // initial hide
$('.entry-title').click(function() {
var clicked = this; // take a reference of clicked element
// to use it within hide() callback scope
// hide all visible sections
$('.toggle-section:visible').hide(function() {
// show the clicked
$(clicked).closest('.post').find('.toggle-section').slideDown();
});
return false;
});
});
</script>