在页面加载时,将某个样式设置为LI元素

时间:2016-01-30 13:02:13

标签: javascript jquery html css html5

我有一个包含以下菜单的页面:

https://jsfiddle.net/dva4zo8t/

根据单击的菜单按钮,颜色会发生变化,我可以“记住”(设置)新页面加载的颜色,如下所示:

$('[id*="button"]').click(function() {
    $('.topmenu-ul li').removeClass();
    $(this).addClass('topmenu-selected' + $('a', this).attr('class'));
});

我还想在页面加载后为LI元素(不同的背景颜色和红色突出显示的文本)设置样式。所以,当我点击“New Appointment”时,在新页面上,LI元素应如下所示:

example

所以我基本上想要的是改变子li的类,就像我使用主按钮一样,例如:

$('#redbutton').addClass('topmenu-selectedred');
$('.topmenu-tab-appointments').show();

3 个答案:

答案 0 :(得分:2)

我创造了一个小提琴,可以让按钮在按下时将其变成背景。

然后当别人被按下时,你会让他们“刷新”。

尝试这个小提琴。

    $(".topmenu-ul li").click(function() {
      $('li > #topmenu-ul').hide();
      $(this).children("ul").toggle();
    });

    $('[id*="button"]').click(function() {
      $('.topmenu-ul li').removeClass();
      $(this).addClass('topmenu-selected' + $('a', this).attr('class'));
    });

    $('.topmenu-ul li a').click(function() {
      $(this).addClass('topmenu-selectedsub');
    });
* {
  margin: 0;
  padding: 0;
  overflow: auto;
}
html,
body {
  height: 100%
}
header,
footer,
article,
section,
hgroup,
nav,
figure {
  display: block
}
body {
  font-size: 1em;
  color: #fcfcfc;
  background-color: #f8f4eb;
  font-family: Verdana, Arial, Helvetica, sans-serif;
}
/*
 *  HTML5 Sections
 */

.header {
  height: 72px;
  margin: 0;
  padding: 0;
  background-color: #fff;
  overflow: hidden;
}
.nav {
  position: relative;
  height: 52px;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
.main {
  position: relative;
  min-height: calc(100% - 124px);
  background-color: #f8f4eb;
}
.aside {
  float: left;
  width: 195px;
  background-color: #ebddca;
  height: 100%;
}
/*
 *  Top Menu Styles
 */

.topmenu {
  background: -webkit-linear-gradient(#858585, #636263);
  border-top: 1px solid #656565;
  border-bottom: 1px solid #3663ab;
  box-shadow: inset 0 1px 0 #a8a8a8;
  height: 20px;
  font-family: Verdana, Arial, Helvetica, sans-serif;
  color: #000
}
.topmenu-header {
  height: 4px;
  background: -webkit-linear-gradient(top, #f5efe4 0%, #d3cdbe 100%);
  border-top: 1px solid #d5cab8
}
.topmenu-subbg {
  padding-left: 5px;
  left: 0;
  width: 100%;
  height: 24px;
  top: 30px;
  background: -webkit-linear-gradient(top, #c8bfb0 0px, #f5efe6 7px);
  border-bottom: 1px solid #d3c7b6
}
.topmenu-ul,
li,
a {
  margin: 0;
  padding: 0;
  cursor: pointer;
}
.topmenu-ul li {
  list-style: none
}
a {
  text-decoration: none;
  color: #000
}
.topmenu-ul > li {
  float: left;
  display: inline;
  list-style: none;
  white-space: nowrap;
  border-right: 1px solid #414141;
  box-shadow: 1px 0 0 0 rgba(165, 162, 165, 1)
}
.topmenu-ul > li a {
  color: #e6e6e6;
  font-size: .7rem;
  line-height: 20px;
  height: 20px;
  display: block;
  padding: 0 20px
}
.topmenu-ul > li a:hover {
  color: #fff
}
.topmenu-ul li ul li a:hover {
  background-color: #f3efe5
}
.topmenu-ul li ul {
  font-size: 0;
  display: none;
  list-style: none;
  position: absolute;
  top: 27px;
  left: -8px;
}
.topmenu-ul li ul li a {
  color: #000;
  line-height: 24px;
  height: 24px;
  font-weight: normal;
}
.topmenu-ul li ul li a:hover {
  color: red;
}
.topmenu-ul li ul li {
  display: inline-block;
  list-style: none;
  white-space: nowrap;
  line-height: 24px;
  height: 24px;
  background: -webkit-linear-gradient(top, #c8bfb0 0px, #f5efe6 7px);
  border-bottom: 1px solid #d3c7b6;
  border-right: 1px solid #d5ccbe
}
.topmenu-ul > [class*=topmenu-selected] > a {
  color: #fff;
}
.topmenu-selectedblue {
  color: #fff;
  font-weight: 700;
  background: -webkit-linear-gradient(#78b1ff, #4881dc)
}
.topmenu-selectedred {
  color: #fff;
  font-weight: 700;
  background: -webkit-linear-gradient(#ff8476, #dc5348)
}
.topmenu-selectedpurple {
  color: #fff;
  font-weight: 700;
  background: -webkit-linear-gradient(#b479ff, #854ade)
}
.topmenu-selectedgreen {
  color: #fff;
  font-weight: 700;
  background: -webkit-linear-gradient(#9dd592, #649f5a)
}
.topmenu-selectedsub {
  background-color: #f3efe5
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="nav">
  <div class="topmenu-header"></div>
  <div class="topmenu">
    <ul class="topmenu-ul">
      <li id="bluebutton"><a class="blue">Home</a>

        <ul id="topmenu-ul" class="topmenu-tab-home">
          <li><a href="{{ route('dashboard') }}">Dashboard</a>
          </li>
        </ul>
      </li>
      <li id="redbutton"><a class="red">Appointments</a>

        <ul id="topmenu-ul" class="topmenu-tab-appointments">
          <li><a href="#">Appointments</a>
          </li>
          <li><a id="new" href="#">New Appointment</a>
          </li>
        </ul>
      </li>
      <li id="greenbutton"><a class="green">Contacts</a>

        <ul id="topmenu-ul" class="topmenu-tab-contacts">
          <li><a href="#">Contacts</a>
          </li>
          <li><a href="#">New Contact</a>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</nav>

修改

无论如何,如果您想在页面加载后执行此操作,可以使用document.ready

$( document ).ready(function() {
  //JUST ADD AN ID TO THE BUTTON AND THIS WILL CHANGE IT´S BACKGROUND AFTER PAGE LOADS
   $("#new").addClass('topmenu-selectedsub');
});

有演示:

$( document ).ready(function() {
  //JUST ADD AN ID TO THE BUTTON AND THIS WILL CHANGE IT´S BACKGROUND AFTER PAGE LOADS
  $('#new').addClass('topmenu-selectedsub');
   $('.topmenu-tab-appointments').show();

});


$(".topmenu-ul li").click(function() {
  $('li > #topmenu-ul').hide();
  $(this).children("ul").toggle();
});

$('[id*="button"]').click(function() {
  $('.topmenu-ul li').removeClass();
  $(this).addClass('topmenu-selected' + $('a', this).attr('class'));
});

$('.topmenu-ul li a').click(function() {
  $(this).addClass('topmenu-selectedsub');
});
* {
  margin: 0;
  padding: 0;
  overflow: auto;
}
html,
body {
  height: 100%
}
header,
footer,
article,
section,
hgroup,
nav,
figure {
  display: block
}
body {
  font-size: 1em;
  color: #fcfcfc;
  background-color: #f8f4eb;
  font-family: Verdana, Arial, Helvetica, sans-serif;
}
/*
 *  HTML5 Sections
 */

.header {
  height: 72px;
  margin: 0;
  padding: 0;
  background-color: #fff;
  overflow: hidden;
}
.nav {
  position: relative;
  height: 52px;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
.main {
  position: relative;
  min-height: calc(100% - 124px);
  background-color: #f8f4eb;
}
.aside {
  float: left;
  width: 195px;
  background-color: #ebddca;
  height: 100%;
}
/*
 *  Top Menu Styles
 */

.topmenu {
  background: -webkit-linear-gradient(#858585, #636263);
  border-top: 1px solid #656565;
  border-bottom: 1px solid #3663ab;
  box-shadow: inset 0 1px 0 #a8a8a8;
  height: 20px;
  font-family: Verdana, Arial, Helvetica, sans-serif;
  color: #000
}
.topmenu-header {
  height: 4px;
  background: -webkit-linear-gradient(top, #f5efe4 0%, #d3cdbe 100%);
  border-top: 1px solid #d5cab8
}
.topmenu-subbg {
  padding-left: 5px;
  left: 0;
  width: 100%;
  height: 24px;
  top: 30px;
  background: -webkit-linear-gradient(top, #c8bfb0 0px, #f5efe6 7px);
  border-bottom: 1px solid #d3c7b6
}
.topmenu-ul,
li,
a {
  margin: 0;
  padding: 0;
  cursor: pointer;
}
.topmenu-ul li {
  list-style: none
}
a {
  text-decoration: none;
  color: #000
}
.topmenu-ul > li {
  float: left;
  display: inline;
  list-style: none;
  white-space: nowrap;
  border-right: 1px solid #414141;
  box-shadow: 1px 0 0 0 rgba(165, 162, 165, 1)
}
.topmenu-ul > li a {
  color: #e6e6e6;
  font-size: .7rem;
  line-height: 20px;
  height: 20px;
  display: block;
  padding: 0 20px
}
.topmenu-ul > li a:hover {
  color: #fff
}
.topmenu-ul li ul li a:hover {
  background-color: #f3efe5
}
.topmenu-ul li ul {
  font-size: 0;
  display: none;
  list-style: none;
  position: absolute;
  top: 27px;
  left: -8px;
}
.topmenu-ul li ul li a {
  color: #000;
  line-height: 24px;
  height: 24px;
  font-weight: normal;
}
.topmenu-ul li ul li a:hover {
  color: red;
}
.topmenu-ul li ul li {
  display: inline-block;
  list-style: none;
  white-space: nowrap;
  line-height: 24px;
  height: 24px;
  background: -webkit-linear-gradient(top, #c8bfb0 0px, #f5efe6 7px);
  border-bottom: 1px solid #d3c7b6;
  border-right: 1px solid #d5ccbe
}
.topmenu-ul > [class*=topmenu-selected] > a {
  color: #fff;
}
.topmenu-selectedblue {
  color: #fff;
  font-weight: 700;
  background: -webkit-linear-gradient(#78b1ff, #4881dc)
}
.topmenu-selectedred {
  color: #fff;
  font-weight: 700;
  background: -webkit-linear-gradient(#ff8476, #dc5348)
}
.topmenu-selectedpurple {
  color: #fff;
  font-weight: 700;
  background: -webkit-linear-gradient(#b479ff, #854ade)
}
.topmenu-selectedgreen {
  color: #fff;
  font-weight: 700;
  background: -webkit-linear-gradient(#9dd592, #649f5a)
}
.topmenu-selectedsub {
  background-color: #f3efe5
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="nav">
  <div class="topmenu-header"></div>
  <div class="topmenu">
    <ul class="topmenu-ul">
      <li id="bluebutton"><a class="blue">Home</a>

        <ul id="topmenu-ul" class="topmenu-tab-home">
          <li><a href="{{ route('dashboard') }}">Dashboard</a>
          </li>
        </ul>
      </li>
      <li id="redbutton"><a class="red">Appointments</a>

        <ul id="topmenu-ul" class="topmenu-tab-appointments">
          <li><a href="#">Appointments</a>
          </li>
          <li><a id="new" href="#">New Appointment</a>
          </li>
        </ul>
      </li>
      <li id="greenbutton"><a class="green">Contacts</a>

        <ul id="topmenu-ul" class="topmenu-tab-contacts">
          <li><a href="#">Contacts</a>
          </li>
          <li><a href="#">New Contact</a>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</nav>

答案 1 :(得分:2)

要执行此操作,您通常会使用服务器端语言在加载页面时设置类(即;在“关于”页面上将类about-page添加到正文,或将类current添加到正文关于链接)。但要使用jQuery,只需要知道页面的URL。

&#13;
&#13;
$(document).on('ready', function(){
  
  var $links = $('nav a'),
      
      links_array = [],
      
      current_url = window.location.pathname,
      
      current_link_idx;
  
  // we dont have an actual url so we'll pretend here 
  // for the sake of the snippet/preview
  current_url = '/about';
  
  
  $links.each(function(){
    links_array.push($(this).attr('href'));
  });
  
  current_link_idx = links_array.indexOf(current_url);
  
  $links.eq(current_link_idx).addClass('current-page');
  
});
&#13;
.current-page {
  color: red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<nav>
  <a href="/about">About</a>
  <a href="/contact">Contact</a>
  <a href="/etc">Etc</a>
</nav>
&#13;
&#13;
&#13;

显然,如果你有复杂的导航/网址,这不是防弹的。您需要对current_url进行一些调整,可能会将其拆分为片段。

不过,这是服务器端的最佳选择。

答案 2 :(得分:1)

查看此工作Demo

 $(".topmenu-ul li").click(function() {
        $('li > #topmenu-ul').hide();
        $(this).children("ul").toggle();
        $(".topmenu-ul li").css("background-color","")
        $(this).css("background-color","red")
    });
$('[id*="button"]').click(function() {
        $('.topmenu-ul li').removeClass();
        $(this).addClass('topmenu-selected' + $('a', this).attr('class'));

    });

    $("#topmenu-ul li a").click(function() {


        $("#topmenu-ul li a").css("background-color","")
        $(this).css("background-color","red")
    });

您可以添加您想添加的颜色。它告诉你,我刚刚告诉你如何做到这一点