简体   繁体   English

单页网站,显示/隐藏div,菜单按钮不会保持选中状态

[英]One page website, show/hide divs, menu button will not stay selected

I'm trying to make a one page website with a menu (pictures, css roll over...), that will display a different div when each menu button is clicked. 我正在尝试制作一个带有菜单的单页网站(图片,css翻转...),当点击每个菜单按钮时,它将显示不同的div。 Only one div will be shown at time though and if one is already open it should be hidden. 虽然只有一个div会被显示,如果一个已经打开,它应该被隐藏。 This is working well. 这很好用。

The problem I am having is that that the menu button which shows the result will not stay selected ie on the same picture as the roll over (hover). 我遇到的问题是显示结果的菜单按钮不会保持选中状态,即与翻转相同的图片(悬停)。

HTML : HTML:

<ul class="menu">
    <li class="home"><a href="javascript:showHide('content1');" title="Home"><span class="displace"></span></a></li>
    <li class="credits"><a href="javascript:showHide('content2');" title="Credits"><span class="displace"></span></a></li>
    <li class="idea"><a href="javascript:showHide('content3');" title="Idea"><span class="displace"></span></a></li>
</ul>

<div id="content1">home text</div>
<div id="content2">credits text1</div>
<div id="content3">idea text</div>​

JS / jQuery : JS / jQuery:

function showHide(d)
{
var onediv = document.getElementById(d);
var divs=['content1','content2','content3'];
for (var i=0;i<divs.length;i++)
  {
  if (onediv != document.getElementById(divs[i]))
    {
    document.getElementById(divs[i]).style.display='none';
    }
  }
onediv.style.display = 'block';
}


$(function stay() {
    $('menu').click(function stay() {
            $('menu').removeClass('selected');
            $(this).addClass('selected');

    });
});
​

Demo: http://jsfiddle.net/anKT3/159/ 演示: http//jsfiddle.net/anKT3/159/

I've tried creating a function to change the class, but I've not had any luck. 我已经尝试创建一个改变课程的功能,但我没有运气。

Here is JS fiddle 这是JS小提琴

$(function stay() {
    $('ul.menu li a').click(function () {
            $('ul.menu li a').removeClass('selected');
            $(this).addClass('selected');

    });
});

Change your stay() function to be as follows: stay()函数更改为如下:

$(function stay() {
    $('.menu li a').click(function stay() {            
            $('.menu li a').removeClass('selected');
            $(this).addClass('selected');                        
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM