jQuery

4.1 简单选择器

  (1) :first 选取第一个元素。

  (2) :last 选取最后一个元素。

  (3) :not(选择器) 选取不满足“选择器”条件的元素

  (4) :even、:odd,选取索引是奇数、偶数的元素

  (5) :eq(索引序号)等于、:gt(索引序号)大于、:lt(索引序号)小于

  (6) $(":header")选取所有的h1……h6元素(*)

  (7) $("div:animated")选取正在执行动画的<div>元素。 (*)

  案例

4.2 相对定位

 

简单选择器


image

(1) :first 选取第一个元素。$("div:first")选取第一个<div>$(".warn:first");

(2) :last 选取最后一个元素。$("div:last")选取最后一个<div>

(3) :not(选择器) 选取不满足“选择器”条件的元素,$("input:not(.myClass)")

选取样式名不是myClass的<input>

显示行号 复制代码 这是一段程序代码。
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.     <title></title>
  6.     <script src="Jqeury/jquery-1.10.2.js" type="text/javascript"></script>
  7.     <script type="text/javascript">
  8.         $(function () {
  9.             //:first 选取第一个元素。
  10.             $("input:first").css("background-color", "blue");
  11.             //:last 选取最后一个元素。
  12.             $("input:last").css("background-color", "gray");
  13.             //:not(选择器) 选取不满足“选择器”条件的元素
  14.             $("input:not(.myClass)").val("不是myClass");
  15.         })
  16.     </script>
  17. </head>
  18. <body>
  19.     <input id="d1" type="text" />first<br />
  20.     <input type="text" /><br />
  21.     <input class="myClass" type="text" /><br />
  22.     <input class="myClass" type="text" /><br />
  23.     <input type="text" /><br />
  24.     <input type="button" value="aaa" /><br />
  25.     <input type="button" value="aaa" /><br />
  26.     <input type="text" /><br />
  27.     <input type="text" />last<br />
  28. </body>
  29. </html>

 

(4) :even、:odd,选取索引是奇数、偶数的元素:$("input:even")选取索引是奇数的<input>
(5) :eq(索引序号)等于、:gt(索引序号)大于、:lt(索引序号)小于 选取索引等于、大于、小于索引序号的元素,比如$("input:lt(5)")选取索引小于5的<input>
(6) $(":header")选取所有的h1……h6元素(*)
(7) $("div:animated")选取正在执行动画的<div>元素。 (*)

image

 

案例


第一行是表头,所以显示大字体(fontSize=30),最后一行是汇总,所以显示红色字体。正文的前三行是前三名,所以显示大的字体(28)表格的奇数行是黄色背景。
用Dom实现;用JQuery实现。对比差异!

 

显示行号 复制代码 这是一段程序代码。
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.     <title></title>
  6.     <script src="Jqeury/jquery-1.10.2.js"></script>
  7.     <script type="text/javascript">
  8.         $(function () {
  9.             //表头
  10.             $("#salary tr:eq(0)").css({ "font-size": "30px", "text-align": "center" });
  11.             //最后1行
  12.             $("#salary tr:last").css("color", "red");
  13.             //排除表头的前3行
  14.             $("#salary tr:not(:first):lt(3)").css("font-size", "22px");
  15.             //排除表头和汇总
  16.             var str = "#salary tr:not(:first):not(:last)";
  17.             //各行变色
  18.             $(str + ":even").css("background-color", "yellow");
  19.             var bgColor;
  20.             //光棒效果
  21.             $(str).mouseover(function () {
  22.                 bgColor = $(this).css("background-color");
  23.                 $(this).css("background-color", "blue");
  24.             }).mouseout(function () {
  25.                 $(this).css("background-color", bgColor);
  26.             })
  27.             //鼠标变小手
  28.             $(str).css("cursor", "pointer");
  29.         })
  30.     </script>
  31. </head>
  32. <body>
  33.     <table id="salary" border="1px" width="300">
  34.         <tbody>
  35.             <tr>
  36.                 <td>姓名</td>
  37.                 <td>年龄</td>
  38.                 <td>工资</td>
  39.             </tr>
  40.             <tr>
  41.                 <td>江户川</td>
  42.                 <td>42</td>
  43.                 <td>30000</td>
  44.             </tr>
  45.             <tr>
  46.                 <td>刘备</td>
  47.                 <td>38</td>
  48.                 <td>25000</td>
  49.             </tr>
  50.             <tr>
  51.                 <td>关羽</td>
  52.                 <td>28</td>
  53.                 <td>15000</td>
  54.             </tr>
  55.             <tr>
  56.                 <td>张飞</td>
  57.                 <td>27</td>
  58.                 <td>8000</td>
  59.             </tr>
  60.             <tr>
  61.                 <td>诸葛亮</td>
  62.                 <td>38</td>
  63.                 <td>22000</td>
  64.             </tr>
  65.             <tr>
  66.                 <td>汇总</td>
  67.                 <td colspan="2">220000</td>
  68.             </tr>
  69.         </tbody>
  70.     </table>
  71. </body>
  72. </html>

1

 

练习


案例:表格隔行变色
案例:前三名粗体显示

 

相对定位



不仅可以使用选择器进行进行绝对定位,还可以进行相对定位,只要在$()指定第二个参数,第二个参数为相对的元素. $("ul", $(this)).css("background", "red");
案例:修改点击行的所有td的背景色

(看上面工资表格那块)

imageimage

原文地址:https://www.cnblogs.com/tangge/p/3180830.html