当在第1页触发onclick事件时,如何在第2页的div中添加类?

时间:2016-02-07 10:22:11

标签: javascript jquery html twitter-bootstrap

索引页面有此

<a href="about.html#collapseOne">Link to About page Panel 1</a>
<a href="about.html#collapseTwo">Link to About page Panel 2</a>
<a href="about.html#collapseThree">Link to About page Panel 3</a>

单击“链接到关于页面面板1或2或3”时(索引页面将关闭),将打开关于打开相应手风琴面板的页面。

JSfiddle for about page如下。

https://jsfiddle.net/Deepika_Rao_PK/nv79rs8b/#&togetherjs=lx2xS3u3Ak

找到解决方案: sessionStorage.setItem not working in firefox / cannot pass id of div from page1 onto page2

2 个答案:

答案 0 :(得分:0)

如果您有两个不同的页面。 您可以在URL中检查哈希,如下所示。

$(document).ready(function(){
  if(window.location.hash =='#collapseOne') {
    $('#collapseOne').addClass('in');
  }
});

答案 1 :(得分:0)

您可以使用CSS :target

CSS

.target {
    display: none;
}

.target:target {
    display: block;
}

HTML

<!-- index page -->
<a href="about.html#collapseOne">Link to About page Panel 1</a>
<a href="about.html#collapseTwo">Link to About page Panel 2</a>
<a href="about.html#collapseThree">Link to About page Panel 3</a>

<!-- page 1  -->
<div class="target" id="collapseOne">

  This content will show when hash is #collapseOne

</div>

<!-- page 2  -->
<div class="target" id="collapseTwo">

  This content will show when hash is #collapseTwo

</div>

<!-- page 3  -->
<div class="target" id="collapseThree">

  This content will show when hash is #collapseThree

</div>