简体   繁体   English

div,显示,隐藏切换。 链接颜色更改

[英]Div,show,hide toggle. with changing link Color

Hey , I tried to make a hide/show toogle for my site. 嘿,我试图为我的网站设置隐藏/显示样式。 And change the link color. 并更改链接颜色。

I know the prob is the input. 我知道概率就是输入。 The linkcolor goes on the divs. linkcolor在div上。 Not on the link. 不在链接上。

Maybe anybody have an good idea. 也许任何人都有一个好主意。 Think i need a new function. 想我需要一个新功能。 (jquery is on) maybe a shorter way is possible. (启用jQuery)可能是更短的方法。

Thanks in advance! 提前致谢!

//javascipt // javascipt的

          function toggleMe(a){
      var e=document.getElementById(a);
      var col=document.getElementById(a);
      if(!e)return true;
      if(e.style.display=="none"){
        e.style.display="block"
        col.style.color="#000000"     
      } else {
        e.style.display="none"
     col.style.color="#000000"
      }
      return true;
    }

//input 

  <div id="topp">

        <div id="tops">

             <li> <a href="www.google" target="_self">Serien</a></li>
                 <li>Architektur</li>
              <li >Portrait</li>
              <li><a href=# onclick="return toggleMe('about')">About Me</a></li>
                <li><a href=#  onclick="return toggleMe('kont')">Kontakt</a></li>
      <li><a href=#  onclick="return toggleMe('imp')">Impressum</a></li>
             <lie><span class="Stil1">Su</span>terrain.de</li></div>

                 </div>     
          </div>
//divs

         <div id="about" style="display:none" onclick="hideBox('about');">
    Lutz Bartelt<br />
    About

         </div>



         <div id="kont" style="display:none" onclick="hideBox('kont');">
    Lutz Bartelt<br />
 <br />
    01522386174<br />
    WhiteWall<br />



     </div>


         <div id="imp" style="display:none" onclick="hideBox('imp');">
    impress
     </div>

Well, I've adapted your html: 好吧,我已经改编了您的html:

<div id="tops">
    <ul>
        <li><a href="www.google" target="_self">Serien</a></li>
        <li>Architektur</li>
        <li>Portrait</li>
        <li><a href="#" name="about">About Me</a></li>
        <li><a href="#" name="kontakt">Kontakt</a></li>
        <li><a href="#" name="impressum">Impressum</a></li>
        <li><span class="Stil1">Su</span>terrain.de</li>
    </ul>
</div>
<div id="panels">
    <div id="about">
        Lutz Bartelt<br />
        About
    </div>

    <div id="kontakt">
        Lutz Bartelt<br />
        <br />
        01522386174<br />
        WhiteWall<br />
    </div>

    <div id="impressum">
        impress
    </div>
</div>

And the following jQuery seems to do what I think you're trying to do: 以下jQuery似乎可以完成我认为您要尝试执行的操作:

$('#tops > ul > li > a').click(

function() {
    var showThis = $(this).attr('name');
    $('.active').removeClass('active');
    $(this).addClass('active');
    $('#panels > div').hide();
    $('#' + showThis).show();
    return false;
});

I used an 'active' class-name for the color changing, which in the demo is defined in CSS as: 我使用了一个'active'类名来进行颜色更改,在演示中,该类名在CSS中定义为:

.active {
    color: #f00;
    font-weight: bold;
}

JS Fiddle demo of the above . 上面的JS Fiddle演示

You forgot to pass the link object so there was no way for you to actually change the color of it: 您忘记了传递链接对象,因此无法实际更改其颜色:

function toggleMe( div, link )
{
  var e = document.getElementById( div );
  if( !e ) return( true );
  if( e.style.display == "none" )
  {
    e.style.display = "block";
    link.style.color = "red";
  }
  else
  {
    e.style.display = "none";
    link.style.color = "blue";
  }
  return( false );
}

<div id="topp">

            <div id="tops">

                 <li> <a href="www.google" target="_self">Serien</a></li>
                     <li>Architektur</li>
                  <li >Portrait</li>
                  <li><a href=# onclick="return toggleMe('about', this)">About Me</a></li>
                    <li><a href=#  onclick="return toggleMe('kont', this)">Kontakt</a></li>
          <li><a href=#  onclick="return toggleMe('imp', this)">Impressum</a></li>
                 <lie><span class="Stil1">Su</span>terrain.de</li></div>

                     </div>     
              </div>
    //divs

             <div id="about" style="display:none" onclick="hideBox('about');">
        Lutz Bartelt<br />
        About

             </div>



             <div id="kont" style="display:none" onclick="hideBox('kont');">
        Lutz Bartelt<br />
     <br />
        01522386174<br />
        WhiteWall<br />



         </div>


             <div id="imp" style="display:none" onclick="hideBox('imp');">
        impress
         </div>

Edit: You may also want to return false at the bottom of the function so the URL isn't actually followed. 编辑:您可能还想在函数底部返回false,以便实际上不遵循URL。

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

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