Jquery show/hide in another page

依然范特西╮ 提交于 2019-12-12 02:38:49

问题


I have 2 html page

in Page 1 there is 2 links "link1" & "link2"

in Page 2 there is also 2 links "link1" & "link2" as well as 2 <div id="pan1"> & <div id="pan2"> <div id="pan1">

& <div id="pan2"> is working as show/hide with jquery

I want when user click on the link1 in page1 it will go to page 2 and div"id=pan1" will show and when user click on the link2 in page1 it will go to page 2 and div"id=pan2" will show.

here is the html code for page 1

<ul class="linkList">
<li><a href="#pan1">Link 1</a></li>
<li><a href="#pan2">Link 2</a></li>
</ul>

here is the code for page 2

html

<ul class="linkList">
<li><a href="#pan1">Link 1</a></li>
<li><a href="#pan2">2</a></li>
</ul>
<div id="pan1" class="switchgroup" style="padding:10px; background-color:#060">div 1</div>
<div id="pan2" class="switchgroup" style="padding:10px; background-color:#936">div 2</div>

css

#pan1, #pan2{
    display:none;   
}

jquery

$(document).ready(function(){
    $('#pan1').show();
    $('.linkList li:first-child a').addClass('active');
    $('.linkList li a').click(function() {
        var tabDivId = this.hash;                              

        $('.linkList li a').removeClass('active');
        $(this).addClass('active');
        //console.log(tabDivId);
        $('.switchgroup').hide();
        $(tabDivId).fadeIn();
        return false;
    });
});

回答1:


This should give you a general idea of how to do it. Please note I haven't tested this code so it might have some minor issues.

Page 1 HTML:

<ul class="linkList">
    <li><a href="page2.html#pan1">Link 1</a></li>
    <li><a href="page2.html#pan2">Link 2</a></li>
</ul>

Page 2 HTML:

<ul class="linkList">
    <li><a href="#pan1" class="panlink">Link 1</a></li>
    <li><a href="#pan2" class="panlink">2</a></li>
</ul>
<div id="pan1" class="switchgroup">div 1</div>
<div id="pan2" class="switchgroup">div 2</div>

Page 2 JS:

$(function() {
    var anc = window.location.href.split('#')[1];
    $('#' + anc + '.switchgroup').show();

    $('a.panlink').click(function() {
        $('.switchgroup').hide();
        $($(this).attr('href')).show();
    });
});

Page 2 CSS:

.switchgroup { display: none; }



回答2:


I don't think you can create function to be implemented on other pages, but in your case you can use parameters:

when you provide the second page's (redirect to the second page) add a query string parameter, or by form's parameters (Post/Get). and on JQuery's ready function add your hide/show code.

from where I understood your question, this should do the work.



来源:https://stackoverflow.com/questions/13428818/jquery-show-hide-in-another-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!