Jquery改变了整个li的类

时间:2016-05-13 12:05:37

标签: jquery

我的JSP中有这段代码:

 <div class="thedescription">
     <ul class="descriptionlanguages">
        <li id="EN_langLinkLi" class="selectedlanguage" >
               <a id="EN_langLink" class="showDifferentLang" href="#">EN</a>
        </li>
        <li id="IT_langLinkLi" class="notSelectedlanguage" >
            <a id="IT_langLink" class="showDifferentLang" href="#">IT</a>
        </li>
    </ul>
   </div>

和脚本,我用来改变整个li的类

 <script type="text/javascript">
      $(document).ready(function(){
                $('li').click(function() {
                      alert ('lsls');
                      $('#thedescription ul li ').each(function(){
                       this.className = 'notSelectedlanguage';
                });
              });
         });
        </script>

但不会改变任何东西!

2 个答案:

答案 0 :(得分:4)

您可以使用addClass()添加要点击liremoveClass()的课程,以便从兄弟姐妹li

中删除此课程

$('li').click(function() {
  $(this).addClass('notSelectedlanguage').siblings().removeClass('notSelectedlanguage');
});
.notSelectedlanguage {
  background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thedescription">
  <ul class="descriptionlanguages">
    <li id="EN_langLinkLi" class="selectedlanguage">
      <a id="EN_langLink" class="showDifferentLang" href="#">EN</a>
    </li>
    <li id="IT_langLinkLi" class="notSelectedlanguage">
      <a id="IT_langLink" class="showDifferentLang" href="#">IT</a>
    </li>
  </ul>
</div>

答案 1 :(得分:1)

您可以尝试--packages

,而不是this.className = 'notSelectedlanguage;