JavaScript Tutorial/jQuery/UL LI — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:25, 26 мая 2010

30. Add class to all children from UL

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
var tmpExample = {
  ready : function() {
    $("ul").children().addClass("tmpChild");
  }
};
$(document).ready(tmpExample.ready);
    </script>
    <style type="text/css">
ul {
    list-style: none;
    margin: 0;
    padding: 0;
}
ul li {
    margin: 1px;
    padding: 3px;
}
li.tmpChild {
    background: #cf0c35;
    color: white;
}
    </style>
  </head>
  <body>
    <ul>
       <li>A</li>
       <li>B</li>
       <li>C</li>
       <li>D</li>
       <li>E</li>
       <li>F</li>
    </ul>
  </body>
</html>


30. Add class to next li

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
var tmpExample = {
  ready : function() {
    $("li#liID").next().addClass("mySiblings");
  }
};
$(document).ready(tmpExample.ready);
    </script>
    <style type="text/css">
ul#uiID {
    list-style: none;
    margin: 0;
    padding: 0;
}
ul#uiID li {
    margin: 1px;
    padding: 3px;
}
li.mySiblings {
    background: #165b91;
    color: white;
}
    </style>
  </head>
  <body>
    <ul id="uiID">
      <li>Red</li>
      <li>Blue</li>
      <li>Green</li>
      <li id="liID">Yellow</li>
      <li>Orange</li>
      <li>Purple</li>
    </ul>
  </body>
</html>


30. Add class to previous li

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
var tmpExample = {
  ready : function() {
    $("li#liID").prev().addClass("mySiblings");
  }
};
$(document).ready(tmpExample.ready);
    </script>
    <style type="text/css">
ul#uiID {
    list-style: none;
    margin: 0;
    padding: 0;
}
ul#uiID li {
    margin: 1px;
    padding: 3px;
}
li.mySiblings {
    background: #165b91;
    color: white;
}
    </style>
  </head>
  <body>
    <ul id="uiID">
      <li>Red</li>
      <li>Blue</li>
      <li>Green</li>
      <li id="liID">Yellow</li>
      <li>Orange</li>
      <li>Purple</li>
    </ul>
  </body>
</html>


30. Add style to li under a UL with certain id

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
var tmpExample = {
  ready : function() {
    $("ul#myStyle").find("li").addClass("tmpFound");
  }
};
$(document).ready(tmpExample.ready);
    </script>
    <style type="text/css">
ul#myStyle {
    list-style: none;
    margin: 0;
    padding: 0;
}
ul#myStyle li {
    margin: 1px;
    padding: 3px;
}
li.tmpFound {
    background: red;
}
    </style>
  </head>
  <body>
  <body>
    <ul id="myStyle">
        <li><a href="http://www.wbex.ru">wbex</a></li>
        <li><a href="http://www.apple.ru">Apple</a></li>
        <li><a href="http://www.jquery.ru">jQuery</a></li>
    </ul>
  </body>
</html>


30. All from next li

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
var tmpExample = {
  ready : function() {
    $("li#liID").nextAll().addClass("mySiblings");
  }
};
$(document).ready(tmpExample.ready);
    </script>
    <style type="text/css">
ul#uiID {
    list-style: none;
    margin: 0;
    padding: 0;
}
ul#uiID li {
    margin: 1px;
    padding: 3px;
}
li.mySiblings {
    background: #165b91;
    color: white;
}
    </style>
  </head>
  <body>
    <ul id="uiID">
      <li>Red</li>
      <li>Blue</li>
      <li>Green</li>
      <li id="liID">Yellow</li>
      <li>Orange</li>
      <li>Purple</li>
    </ul>
  </body>
</html>


30. All li under ul but not a class name

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
var tmpExample = {
  ready : function() {
    $("ul#uiID li").not("li.liClass2").addClass("justAdd");
  }
};
$(document).ready(tmpExample.ready);
    </script>
    <style type="text/css">
ul#uiID {
    list-style: none;
    margin: 0;
    padding: 0;
}
ul#uiID li {
    margin: 1px;
    padding: 3px;
}
li.justAdd {
    background: rgb(112, 122, 122);
}
    </style>
  </head>
  <body id="tmpExample">
    <ul id="uiID">
      <li class="liClass1">A</li>
      <li class="liClass1">B</li>
      <li class="liClass1">C</li>
      <li class="liClass1">D</li>
      <li class="liClass2">E</li>
      <li class="liClass2">F</li>
      <li class="liClass2">G</li>
      <li class="liClass2">H</li>
      <li class="liClass3">I</li>
      <li class="liClass3">J</li>
    </ul>
  </body>
</html>


30. All previous from a LI tag

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
var tmpExample = {
  ready : function() {
    $("li#liID").prevAll().addClass("mySiblings");
  }
};
$(document).ready(tmpExample.ready);
    </script>
    <style type="text/css">
ul#uiID {
    list-style: none;
    margin: 0;
    padding: 0;
}
ul#uiID li {
    margin: 1px;
    padding: 3px;
}
li.mySiblings {
    background: #165b91;
    color: white;
}
    </style>
  </head>
  <body>
    <ul id="uiID">
      <li>Red</li>
      <li>Blue</li>
      <li>Green</li>
      <li id="liID">Yellow</li>
      <li>Orange</li>
      <li>Purple</li>
    </ul>
  </body>
</html>


30. Click to toggle highlight on the list item.

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                
            $("li").toggle(
              function () {
                $(this).css("list-style-type", "disc")
                       .css("color", "blue");
              },
              function () {
                $(this).css({"list-style-type":"", "color":""});
              }
            );

        });
    </script>
  </head>
  <body>
    <body>
         <ul>
            <li>A</li>
            <li>B</li>
            <li>C</li>
            <li>D</li>
         </ul>
       
                         
    </body>
</html>


30. Finds the second li in each matched ul and notes it.

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                $("ul li:nth-child(2)").append("<span> - 2nd!</span>");
        });
    </script>
  </head>
  <body>
    <body>
      <div>
        <ul>
            <li>A</li>
            <li>B</li>
            <li>C</li>
        </ul>
      </div>
      <div>
        <ul>
            <li>D</li>
            <li>E</li>
            <li>F</li>
            <li>G</li>
            
        </ul>
      </div>
      <div>
        <ul>
            <li>H</li>
            <li>I</li>
            <li>J</li>
            <li>K</li>
        </ul>
      </div>
    </body>
</html>


30. Get LI one by one

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
           var mappedItems = $("li").map(function (index) {
              var replacement = $("<li>").text($(this).text()).get(0);
              $(replacement).text($(replacement).text().toUpperCase());
              return replacement;
           });
           $("#results").append(mappedItems);
        });
    </script>
  </head>
  <body>
    <body>
      <ul>
        <li>asdf</li>
        <li>asdf</li>
        <li>asdf</li>
      </ul>
      <ul id="results">
      </ul>
  </body>
</html>


30. Get parent for LI

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
var myDemo = {
  ready : function() {
    $("li.liClassName3").parents("div#uiIDWrapper").addClass("tmpParent");
  }
};
$(document).ready(myDemo.ready);
    </script>
    <style type="text/css">
ul#uiID {
    list-style: none;
    margin: 0;
    padding: 0;
}
ul#uiID li {
    margin: 1px;
    padding: 3px;
}
div#uiIDWrapper {
    padding: 5px;
    border: 1px solid rgb(200, 200, 200);    
    background: rgb(240, 240, 240);
}
body#myDemo div.tmpParent {
    background: rgb(174, 211, 248);
}
    </style>
  </head>
  <body id="myDemo">
    
    <div id="uiIDWrapper">
      <ul id="uiID">
        <li>A</li>
        <li class="liClassName">B</li>
        <li class="liClassName">C</li>
        <li>D</li>
        <li class="liClassName2">E</li>
        <li>F</li>
        <li class="liClassName3">G</li>
      </ul>
    </div>
  </body>
</html>


30. To add a special style to list item that are being hovered over

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                
            $("li").hover(
              function () {
                $(this).append($("<span>*</span>"));
              }, 
              function () {
                $(this).find("span:last").remove();
              }
            );

        });
    </script>
  </head>
  <body>
    <body>
         <ul>
            <li>A</li>
            <li>B</li>
            <li>C</li>
            <li>D</li>
         </ul>
       
                         
    </body>
</html>