在页面加载时展开切换的Div

时间:2015-08-06 12:49:16

标签: javascript jquery html css

我有一个页面,其中包含许多显示内容的折叠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 -->

1 个答案:

答案 0 :(得分:0)

从网址获取ID,然后调用点击功能在文档就绪

上打开它
$(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");
});