我有以下 jquery 可以展开和折叠我页面上的部分。它还将记住上次扩展的部分,并在重新加载页面时应用该部分。
我想要做的是在我的页面上添加一个“全部展开”和“全部折叠”链接,但我不确定脚本应该是什么样子?有什么帮助吗?
<script type='text/javascript'>
$(function () {
$(".toggle").hide(); //Hide .toggles
$(window).load(function () {
$('.toggle').not(':hidden').prev('.trigger').trigger("click");
});
$('.trigger').each(function () { //For each .trigger
var theActive = $.cookie($(this).attr('id')); //Retrieve the cookies
if (theActive) { //Verify if cookies exist
$('#' + theActive).next(".toggle").slideDown(0); //And slide down the respective .toggle
}
});
$(".trigger").toggle( //Toggle permits alternate clicks
function () {
$(this).next('.toggle').slideDown('slow'); //On odd clicks, .toggle slides down...
$.cookie($(this).attr('id'), $(this).attr('id')); //...and the cookie is set by its ids.
}, function () {
$(this).next('.toggle').slideUp('slow'); //On even clicks, .toggle slides up...
$.cookie($(this).attr('id'), null); //...and the cookie is deleted.
});
});
第 1 节
<div class = "toggle">
Some stuff
</div>
<h3 class = "trigger" id="H1">Section 2</h3>
<div class = "toggle">
More stuff
</div>