4

所以基本上我有两个不同的 HTML 页面。我想创建一个函数,当单击第一个 html 页面上的按钮时,它将显示/隐藏第二个 html 上的 div 元素。

这可以用javascript完成吗?

提前致谢

4

3 回答 3

1

正如@roXon 提到的一个选项,您可以使用 HTML5 存储来做到这一点。

例如

if(typeof(Storage)!=="undefined")
  {
     $('#FirstClick').on('click',function(){
        localStorage.clickedDiv = $(this).attr('id');
        $(this).hide();
     });
  }
else
  {
     $(this).hide();
  }

然后在第二页。

$(function(){
   $(localStorage.clickedDiv).hide();
});

或者为了更向后的浏览器支持,您可以使用 cookie。

于 2013-02-24T22:11:20.370 回答
0

我不确定你所说的第二个 html 页面是什么意思,也许你的意思是内容?使用 jQuery 可以做到这一点……例如……

<script type="text/javascript">

$(document).ready(function(){

        $(".slidingDiv").hide();
        $(".show_hide").show();

    $('.show_hide').click(function(){
    $(".slidingDiv").slideToggle();
    });

});

</script>

<a href="#" class="show_hide">Show/hide</a>
<div class="slidingDiv">
<a href="#" class="show_hide">hide</a></div>

css

.slidingDiv {
    height:300px;
    background-color: #99CCFF;
    padding:20px;
    margin-top:10px;
    border-bottom:5px solid #3399FF;
}

.show_hide {
    display:none;
}

也许您的意思是在第二个页面上加载内容(或与之交互)请参阅http://api.jquery.com/load/和问题:Load content of a div on another page

于 2013-02-24T21:56:52.127 回答
0

如果浏览器支持 HTML5 存储,您可以保存一个变量 http://www.w3schools.com/html/html5_webstorage.asp

否则,您需要传递 GET 参数或 cookie。

于 2013-02-24T22:01:28.560 回答