如何根据重新加载时选择的菜单显示div的内容

时间:2015-03-24 17:44:24

标签: javascript html

我有一个页面,最初显示页面上第一个div的内容, 基于内部样式表。根据菜单选择它成功 显示所选的div并使用名为foo的函数隐藏其他div。 每个div都有指向外部页面的链接。我遇到的问题是,当 用户选择一个链接,然后点击我的页面显示第一个的后退箭头 在页面上显示div并显示菜单上的上一个选项。我想要 显示菜单中显示的div。我希望这是有道理的。 附件是我的代码和页面使用的外部样式表。

<!DOCTYPE HTML>
<html><!-- InstanceBegin template="/Templates/Ricks Universe.dwt" codeOutsideHTMLIsLocked="false" -->
<head>

<!-- InstanceBeginEditable name="doctitle" -->  
<title>Fun and Games</title>
<!-- InstanceEndEditable --> 
<meta charset="utf-8">
<meta name="viewport" content="width=device-width  initial-scale 1.0 minimum-scale 1.0" />

<link rel="stylesheet" href="ricks.css" type="text/css" media="screen" title="Ricks Style Sheet"/>
<link rel="stylesheet" href="hh.css" type="text/css" media="handheld"/>
<link rel="stylesheet" media="print" type="text/css" href="print.css" />   



<!-- InstanceBeginEditable name="head" -->    
<style type="text/css">
#Games{display:inline;}
#Humor{display:none;}
</style>

<script type="text/javascript">

  function foo(option)
  {
    var n = option.search(".HTM"); // Check for external file to link to;

    if (n > -1)  // If true...
    {

    window.location.href=option;
    }
    for (i in divs)   //loop thru divs table
    {
    x=divs[i].substr(1) //Get value for option without #
    z=document.getElementById(x) // Locate div

    if (option == divs[i])  //On match to table...
    {
    z.style.display= "inline" //Display div on page
    } else
        {
    z.style.display="none"    //On no match hide div on page
    }
  }
  }

</script>

<!-- InstanceEndEditable -->

<!-- InstanceParam name="home" type="boolean" value="false" --> 

  <script type="text/javascript">
    function displaymenu() {

    document.getElementById("content").style.display="none";
    document.getElementById("header").style.display="none";
    document.getElementById("footer").style.display="none";
    document.getElementById("menu").style.display ='inline';
    document.getElementById("menu").style.width="100%"; 
    document.getElementById("menu").style["text-align"]='center';
    document.getElementById("menu").style.height="300%";
    document.getElementById("menu").style.zoom="300%";

};

</script>              

<style type="text/css">
<!--

-->@media only screen 
    and (max-device-width : 667px) 
    and (orientation : portrait) 
    and (-webkit-min-device-pixel-ratio : 2)

    {
        .sidebar1 {display:none;}
        .header {display:none;}
        .footer {display: none;}
        .pagepics {display:none;}
        .menulink {display:inline;}
        .content {height:100%;
                  width:100%;}
        .textSmall{
            font-size:x-large;
            font-weight:bold;
    }
    #titlex {display:inline;}
    .bigpic {
        width:400;
        margin-left:0px;
    }

</style><!--[if lte IE 7]>
<style>
.content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */
ul.nav a { zoom: 1; }  /* the zoom property gives IE the hasLayout trigger it needs to correct extra whiltespace between the links */
</style>
<![endif]--></head>

<body>
<!-- InstanceBeginEditable name="onload" -->
<script type="text/javascript">

var string ="#Games,#Humor"; //Load table for foo to match against
var divs = new Array();
  divs = string.split(","); 


var opt = document.getElementById("StoreDirectory").selected,value; // Determine current selected value;

foo("opt"); //Display selected section and hide other;
  </script>
<!-- InstanceEndEditable --> 


<div class="container" id="wrapper">

  <div class="header" id="header">

  <p class="clock"> 


            <iframe src="http://free.timeanddate.com/clock/i425m2zy/n179/tct/pct/ahl/avt/tt0/ta1" frameborder="0" width="378" height="19"             allowTransparency="true"></iframe></p><br/><br/>


            <!-- InstanceBeginEditable name="title" -->
            <p class="title">Fun and Games</p>
            <!-- InstanceEndEditable -->


    <!-- end .header --></div>

  <div class="sidebar1" id="menu">
    <ul class="nav">
<li><a href="MEETDOGS.HTM">Meet the dogs</a></li> 
<li><a href="DOGPAGE.HTML">Rick's Dog Page</a></li>   
<li><a href="ART.HTM">Art</a></li> 
<li><a href="PARKING.HTM">Automotive</a></li>
<li><a href="BABY.HTM">Babies</a></li>
<li><a href="BOUTIQUE.HTM">The Boutique</a></li>
<li><a href="CATLG.HTM">Catalogs</a></li>
<li><a href="CHARITY.HTM">Charities</a></li>
<li><a href="COMPUTE.HTM">Computing</a></li>
<li><a href="DAYCARE.HTM">Day Care</a></li>
<li><a href="DEPT.HTM">Department Stores</a></li>
<li><a href="DOGCOMP.HTM">Dogs In Competition</a></li>
<li><a href="DRUGS.HTM">Drug Stores</a></li>
<li><a href="EDUCATE.HTM">Education</a></li>
<li><a href="ELECT.HTM">Electronics</a></li>
<li><a href="EMPLOY.HTM">Employment</a></li>
<li><a href="ENTERT.HTM">Entertainment</a></li>
<li><a href="MONEY.HTM">Financial</a></li>
<li><a href="FITNESS.HTM">Fitness</a></li>
<li><a href="FLOWERS.HTM">Flowers</a></li>
<li><a href="FOOD.HTM">Food</a></li>
<li><a href="FUNGAME.HTM">Fun and Games</a></li>
<li><a href="GARDEN.HTM">Gardening</a></li>
<li><a href="GOVMENT.HTM">Government</a></li>
<li><a href="CARDS.HTM">Greeting Cards</a></li> 
<li><a href="GROCERY.HTM">Groceries</a></li>
<li><a href="HEALTH.HTM">Health</a></li>
<li><a href="HOBBY.HTM">Hobbies</a></li> 
<li><a href="HARDWARE.HTM">Home Improvement</a></li>
<li><a href="INFO.HTM">Information</a></li>
<li><a href="INTERNET.HTM">Internet and More</a></li>
<li><a href="JEWELRY.HTM">Jewelry</a></li>
<li><a href="BOOKS.HTM">Literature</a></li>
<li><a href="MAPDIR.HTM">Maps and Directions</a></li>
<li><a href="RECORDS.HTM">Music and Video</a></li>
<li><a href="MOVIES.HTM">Movies</a></li>
<li><a href="NEWS.HTM">News Stand</a></li>
<li><a href="OFFICE.HTM">Office Supplies</a></li>
<li><a href="PEOPLENET.HTM">People</a></li>
<li><a href="PETS.HTM">Pets</a></li>
<li><a href="PHONE.HTM">Phones</a></li>
<li><a href="PHOTO.HTM">Photography</a></li>
<li><a href="POST.HTM">Post Office</a></li>
<li><a href="PRICES.HTM">Price Comparison</a></li>
<li><a href="REALEST.HTM">Real Estate</a></li>
<li><a href="SECURITY.HTM">Security</a></li>
<li><a href="SEARCHNET.HTM">Searches</a></li>
<li><a href="SENIOR.HTM">Senior Page</a></li>
<li><a href="SHOES.HTM">Shoe Store</a></li>
<li><a href="SHOPPING.HTM">Shopping Clubs</a></li>
<li><a href="SPORTING.HTM">Sports</a></li>
<li><a href="TOYS.HTM">Toys and Games</a></li>
<li><a href="TV.HTM">Television</a></li>
<li><a href="TRAVEL.HTM">Travel</a></li>
</ul>

     <!-- end .sidebar1 --></div>


  <div class="content" id="content">
  <p class="menulink" > <a onclick="displaymenu()" href="javascript:void(0);" >     
     <span>Menu</span></a></p>



  <script type="text/javascript">
  var head = '<br/><br/><h3 id = "titlex">';
  var title=document.title;
  var trail = '</h3>'
  var heading  = head + title + trail;
  document.write(heading);
  </script>



    <!-- InstanceBeginEditable name="content" -->
  <p><img  class="pagepics" style="width:250px;"src="Pictures/weimball.jpg" alt="weimball.jpg" /></p>        
        <div id="form" class="form">
      <form action="">
        <h5>Page Directory</h5>
        <select  id="StoreDirectory" name="StoreDirectory" onchange="foo(this.value)">
          <option value="#Games">Games</option>
          <option value="#Humor">Humor</option>
         </select>
      </form>
    </div>   


            <div class="d1">

   <div  <a id="Games"  name="Games"></a>        
  <h2>Games</h2>
  <ul>
    <li><a href="http://www.addictinggames.com/">Addicting Games</a></li>
    <li><a href="http://www.bestcrosswords.com/">BestCrosswords.com</a></li>
    <li><a href="http://www.bingozone.com">The Bingo Zone</a></li>
    <li><a href="http://blufr.com/">blufr</a>A trivia game challenging you to identify statements as true or false</li>
    <li><a href="http://www.ababasoft.com/flash_games/">Free Flash Online Games</a></li>
    <li><a href="http://www.freearcade.com/">FreeArcade.com</a></li>
    <li><a href="http://www.gamespot.com/">Gamespot</a></li>
    <li><a href="http://www.itsyourturn.com/"> It's Your Turn</a>Chess, Checkers, Backgammon and more</li>
    <li><a href="https://www.soe.com/">Ultimate Online Gaming</a></li>
    <li><a href="http://www.jigzone.com/">JigZone</a>Jigsaw Puzzle Paradise</li>
    <li><a href="http://www.play-free-online-games.net/">Play-Free-Online-Games.net</a>Multi Player Games on the Internet</li>
    <li><a href="http://www.puzzlechoice.com/">Puzzle Choice</a></li>
    <li><a href="http://www.puzzleconnection.com/">The Puzzle Connection</a></li>
    <li><a href="http://www.websudoku.com/">Web Sudoku</a></li>
     </ul>
  </div>

  <div <a id="Humor"  name="Humor"></a> 
  <h2>Humor</h2>
  <ul>
    <li><a href="http://www.break.com/">Break</a>Funny Pics, Hot Chicks, and Cool Flicks</li>
    <li><a href="http://dumbcriminalacts.com/">Dumb Criminal Acts!</a></li>
    <li><a href="http://www.darwinawards.com/">The Darwin Awards</a>Improvement of the human genome by honoring those who accidentally kill themselves in really stupid ways</li>
    <li><a href="http://www.thepayback.com/">The Pay Back</a>Get revenge with this collection of revenge ideas, pranks, and practical jokes</li>
    <li><a href="http://www.funnypracticaljokes.com/">Practical Jokes</a></li>
  </ul>
  </div>
            </div>


        <!-- InstanceEndEditable -->
    <!-- end .content --></div>


  <div class="footer" id="footer">







        <a class="sitemap" href="SiteMap.htm">Site map</a> &nbsp;&brvbar;&nbsp;<a href="index.html">&nbsp;Home</a >



    <!-- end .footer --></div>

  <!-- end .container --></div>

样式表

body {
    font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
    background: #4E5869;
    margin: 0;
    padding: 0;
    color: #000;
}

/* ~~ Element/tag selectors ~~ */
ul, ol, dl { /* Due to variations between browsers, it's best practices to zero padding and margin on lists. For consistency, you can either specify the amounts you want here, or on the list items (LI, DT, DD) they contain. Remember that what you do here will cascade to the .nav list unless you write a more specific selector. */
    padding: 0;
    margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
    margin-top: 0;   /* removing the top margin gets around an issue where margins can escape from their containing div. The remaining bottom margin will hold it away from any elements that follow. */
    padding-right: 15px;
    padding-left: 15px; /* adding the padding to the sides of the elements within the divs, instead of the divs themselves, gets rid of any box model math. A nested div with side padding can also be used as an alternate method. */
}
a img { /* this selector removes the default blue border displayed in some browsers around an image when it is surrounded by a link */
    border: none;
}

/* ~~ Styling for your site's links must remain in this order - including the group of selectors that create the hover effect. ~~ */
a:link {
    color:#00000;
    text-decoration: underline; /* unless you style your links to look extremely unique, it's best to provide underlines for quick visual identification */
}
a:visited {
    color: #000000;
    text-decoration: underline;
}

/* ~~ this container surrounds all other divs giving them their percentage-based width ~~ */
.container {
    border:solid;
    border-width:10px;
    border-color:#008800;
    width: 80%;
    max-width: 1260px;/* a max-width may be desirable to keep this layout from getting too wide on a large monitor. This keeps line length more readable. IE6 does not respect this declaration. */
    min-width: 780px;/* a min-width may be desirable to keep this layout from getting too narrow. This keeps line length more readable in the side columns. IE6 does not respect this declaration. */
    background: #FFF;
    margin: 0 auto; /* the auto value on the sides, coupled with the width, centers the layout. It is not needed if you set the .container's width to 100%. */
    overflow:visible;
}


/* ~~ the header is not given a width. It will extend the full width of your layout. It contains an image placeholder that should be replaced with your own linked logo ~~ */
.header {
    background: #227Dff;
    margin-bottom:-20px;
    height:150px;
}

/* ~~ These are the columns for the layout. ~~ 

1) Padding is only placed on the top and/or bottom of the divs. The elements within these divs have padding on their sides. This saves you from any "box model math". Keep in mind, if you add any side padding or border to the div itself, it will be added to the width you define to create the *total* width. You may also choose to remove the padding on the element in the div and place a second div within it with no width and the padding necessary for your design.

2) No margin has been given to the columns since they are all floated. If you must add margin, avoid placing it on the side you're floating toward (for example: a right margin on a div set to float right). Many times, padding can be used instead. For divs where this rule must be broken, you should add a "display:inline" declaration to the div's rule to tame a bug where some versions of Internet Explorer double the margin.

3) Since classes can be used multiple times in a document (and an element can also have multiple classes applied), the columns have been assigned class names instead of IDs. For example, two sidebar divs could be stacked if necessary. These can very easily be changed to IDs if that's your preference, as long as you'll only be using them once per document.

4) If you prefer your nav on the right instead of the left, simply float these columns the opposite direction (all right instead of all left) and they'll render in reverse order. There's no need to move the divs around in the HTML source.

*/
.sidebar1 {
    float: left;
    width: 20%;
    background: #93A5C4;
    padding-bottom: 10px;
    height:600px;
    display:block;
    overflow:auto;
    margin-bottom: -10px;
}
.content {
    padding: 10px 0;
    width: 80%;
    float: left;
    height:600px;
    overflow:auto;

    background-color:#00aaFF;
}

/* ~~ This grouped selector gives the lists in the .content area space ~~ */
.content ul, .content ol { 
    padding: 0 15px 15px 40px; /* this padding mirrors the right padding in the headings and paragraph rule above. Padding was placed on the bottom for space between other elements on the lists and on the left to create the indention. These may be adjusted as you wish. */
}

/* ~~ The navigation list styles (can be removed if you choose to use a premade flyout menu like Spry) ~~ */
ul.nav {
    list-style: none; /* this removes the list marker */
    border-top: 1px solid #666; /* this creates the top border for the links - all others are placed using a bottom border on the LI */
    margin-bottom: 15px; /* this creates the space between the navigation on the content below */
}
ul.nav li {
    border-bottom: 2px inset #666; /* this creates the button separation */
}
ul.nav a, ul.nav a:visited { /* grouping these selectors makes sure that your links retain their button look even after being visited */
    padding: 5px 5px 5px 15px;
    display: block; /* this gives the link block properties causing it to fill the whole LI containing it. This causes the entire area to react to a mouse click. */
    text-decoration: none;
    background: #00A;
    color: #FFF;
}
ul.nav a:hover, ul.nav a:active, ul.nav a:focus { /* this changes the background and text color for both mouse and keyboard navigators */
    background: #008800;
    color: #FFF;
}

/* ~~ The footer ~~ */
.footer {

    padding: 10px 0;
    background: #6F7D94;
    position: relative;/* this gives IE6 hasLayout to properly clear */
    clear: both; /* this clear property forces the .container to understand where the columns end and contain them */
    text-align:center;
    color: #fff;
    font-size:xx-small;
}

/* ~~ miscellaneous float/clear classes ~~ */
.fltrt {  /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
    float: right;
    margin-left: 8px;
}
.fltlft { /* this class can be used to float an element left in your page. The floated element must precede the element it should be next to on the page. */
    float: left;
    margin-right: 8px;
}
.clearfloat { /* this class can be placed on a <br /> or empty div as the final element following the last floated div (within the #container) if the #footer is removed or taken out of the #container */
    clear:both;
    height:0;
    font-size: 1px;
    line-height: 0px;
}
.title {
    position: relative; top: -25pt;
    text-align: center;
    color: #00000;
    font-size:48px;
    font-weight:bold;
    font-family:"Times New Roman",Georgia,Serif;
    padding-bottom::10px;

    clear:both;
}

    p.a8 {

    float:right;
    padding-right: 10px;
}
#weather { 
    margin:20px;
    float: right;


}
#google { 
    padding-bottom: 20px;   
    text-align=center;
    clear;
}

td {text-align:center;}


#mapq {
    padding-top: 50px;  
    padding-bottom: 30px;   
    clear;
    float:left;
    text-align:center;
}

#ring {
    text-align: center;
}


.AllDogs {
    text-align: center;
}


#center {
    clear;
    padding-left: 50px;
    padding-bottom: 30px;
    text-align: center;
}

.center {
    clear;
    padding-left: 50px;
    padding-bottom: 30px;
    text-align: center;
}

p img {float:right;}
div img {float:right;}

.iconimg {
         height:50px;
         width:50px;
}

.form {text-align:center};
}

.form h5 {
    text-align:center;
    font-size:large;
}

 ul li a {color:#000;}
 .textSmall{font-size:small; color:#000;}

 .c20 li a{color:#000;}

 div p a {color:#000; font-size:small;}
 .links{padding-left:60px;}

 ul {list-style-image:url(Pictures/bullet-green.png); }
  li a {padding-right: 10px; font-weight:bold;}
  h2 {color:#0000a2;}

.menulink {display:none;}
.menulink span {
    font-size:xx-large;
    color:#000;
    font-weight:bold;
}

#titlex {display:none;}

table {

    table-layout:fixed;
}




td {
    width:193px;
    vertical-align:top;
    text-align:center;
    padding: 10px;
    cellpadding: 50px;
}

td a img{
    border:0px;
    width:75px;
    height:50px;
}

.clock {
    float:left; 
    color: #fff;
}
.sitemap  {
    color:#fff;
} 


.pagepics{
    display:inline;
    margin-right:10px;}   

.icontext {
    padding-left:140px;
    vertical-align:top;
    padding-top:0px;
    margin-top:0px; 
} 

.pic {width:200px;

border:10px;

}

.bigpic {
    float:left;
    padding:0px;
    width:600px;
    margin-left:150px;
    border: 1 solid black;
}
#pix {margin-left:100px;}

2 个答案:

答案 0 :(得分:0)

要做到这一点,您需要记住用户选择的内容。一种方法是使用cookie。

这是我用来处理cookie的几个javascript函数。

function createCookie(name, value, days) {
    var expires;

    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = escape(name) + "=";
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        while (cookie.charAt(0) === ' ')
            cookie = cookie.substring(1, cookie.length);

        if (cookie.indexOf(nameEQ) === 0)
            return unescape(cookie.substring(nameEQ.length, cookie.length));
    }
    return null;
}

在你的foo()函数中调用createCookie()来保存所选的选项。然后添加一个函数句柄页面加载事件。在那里,调用readCookie()来获取所选的选项以确定要显示的div。

答案 1 :(得分:0)

我的坏!我只是以某种方式删除了脚本中最重要的一行:

z.style.display= "inline"

再次感谢您的帮助。