[英]jquery div expand and collapse issue
current code loads div as collapsed and then on click it works as expand/ collapse当前代码将 div 加载为折叠状态,然后单击它作为展开/折叠
i want whenever page loads it should open as expanded div and then on click it should work as collapse/expand.我希望每当页面加载时,它都应该以展开的 div 的形式打开,然后在单击时它应该以折叠/展开的形式工作。
here is current code:-这是当前代码:-
$(".header").click(function () { $header = $(this); //getting the next element $content = $header.next(); //open up the content needed - toggle the slide- if visible, slide up, if not slidedown. $content.slideToggle(500, function () { //execute this after slideToggle is done //change text of header based on visibility of content div $header.text(function () { //change text based on condition return $content.is(":visible") ? "Collapse" : "Expand"; }); }); });
.container { width:100%; border:1px solid #d3d3d3; } .container div { width:100%; } .container .header { background-color:#d3d3d3; padding: 2px; cursor: pointer; font-weight: bold; } .container .content { display: none; padding : 5px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="header"><span>Expand</span> </div> <div class="content"> <ul> <li>This is just some random content.</li> <li>This is just some random content.</li> <li>This is just some random content.</li> <li>This is just some random content.</li> </ul> </div> </div>
You need to update just 2 lines您只需要更新 2 行
<div class="header"><span>Collapse</span>
And和
.container .content {
display: block; // none in your code
padding : 5px;
}
Please run the following code请运行以下代码
$(".header").click(function () { $header = $(this); //getting the next element $content = $header.next(); //open up the content needed - toggle the slide- if visible, slide up, if not slidedown. $content.slideToggle(500, function () { //execute this after slideToggle is done //change text of header based on visibility of content div $header.text(function () { //change text based on condition return $content.is(":visible") ? "Collapse" : "Expand"; }); }); });
.container { width:100%; border:1px solid #d3d3d3; } .container div { width:100%; } .container .header { background-color:#d3d3d3; padding: 2px; cursor: pointer; font-weight: bold; } .container .content { display: block; padding : 5px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="header"><span>Collapse</span> </div> <div class="content"> <ul> <li>This is just some random content.</li> <li>This is just some random content.</li> <li>This is just some random content.</li> <li>This is just some random content.</li> </ul> </div> </div>
You can try:你可以试试:
$(document).ready(function(){
$(".header").trigger("click");
})
This will trigger the click event once the whole document has been loaded entirely.一旦整个文档完全加载,这将触发点击事件。 Sorry I have to write this on answer instead of comments, so it can be read easily.
对不起,我必须在答案而不是评论上写这个,所以它可以很容易地阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.