我有一个页面,其中有许多折叠的DIV显示内容。我希望能够链接到此页面并已打开相关的DIV。例如。 <a href="\page#B">Read about Section 2</a>"。这是我必须控制折叠和扩展的代码。

<script>
jQuery(function ($) {
$(".header").click(function () {

$header = $(this);
$span = $header.find(">:first-child"),
//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)
if ($span.text() == "-")
        $span.text("+")
    else {
        $span.text("-")
    }
});
});
</script>

<div class="container">
<div class="header" id="A">
    <span>+</span>
          <h2>Section1</h2>
          </div>
<div class="content" id="B">
          I'm the content for section 1.
</div>
<div class="header">
    <span>+</span>
          <h2>Section 2</h2>
          </div>
<div class="content">
          I'm the content for Section 2
</div>

</div> <!--- /container -->

最佳答案

从网址获取ID,然后调用click函数以在准备好文档时将其打开

$(document).ready(function(){
var id  = location.search.split('page=')[1];
//find div of according clickable header. not sure which is in your code
$($("#"+id).prev()).trigger("click");
});

关于javascript - 在页面加载时展开切换的Div,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31856260/

10-12 13:14