jQuery学习笔记

1. jQuery的属性操作

1.1. html()、text()、val()

day04_jQuery学习笔记(下) - 图1

html() 它可以设置和获取起始标签和结束标签中的内容。 跟 dom 属性 innerHTML 一样。

text() 它可以设置和获取起始标签和结束标签中的文本。 跟 dom 属性 innerText 一样。

val() 它可以设置和获取表单项的 value 属性值。 跟 dom 属性 value 一样

  1. <!DOCTYPE html>
  2. <html lang="zh_CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script type="text/javascript" src="script/jquery-1.7.2.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. // 不传参数,是获取,传递参数是设置
  10. // alert( $("div").html() );// 获取
  11. // $("div").html("<h1>我是div中的标题 1</h1>");// 设置
  12. // 不传参数,是获取,传递参数是设置
  13. // alert( $("div").text() ); // 获取
  14. // $("div").text("<h1>我是div中的标题 1</h1>"); // 设置
  15. // 不传参数,是获取,传递参数是设置
  16. $("button").click(function () {
  17. alert($("#username").val());//获取
  18. $("#username").val("超级程序猿");// 设置
  19. });
  20. });
  21. </script>
  22. </head>
  23. <body>
  24. <div>我是div标签 <span>我是div中的span</span></div>
  25. <input type="text" name="username" id="username" />
  26. <button>操作输入框</button>
  27. </body>
  28. </html>

val 方法还同时设置多个表单项的选中状态:

  1. <!DOCTYPE html>
  2. <html lang="zh_CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script type="text/javascript" src="script/jquery-1.7.2.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. /*
  10. // 批量操作单选
  11. $(":radio").val(["radio2"]);
  12. // 批量操作筛选框的选中状态
  13. $(":checkbox").val(["checkbox3","checkbox2"]);
  14. // 批量操作多选的下拉框选中状态
  15. $("#multiple").val(["mul2","mul3","mul4"]);
  16. // 操作单选的下拉框选中状态
  17. $("#single").val(["sin2"]);
  18. */
  19. $("#multiple,#single,:radio,:checkbox").val(["radio2","checkbox1","checkbox3","mul1","mul4","sin3"]);
  20. });
  21. </script>
  22. </head>
  23. <body>
  24. <body>
  25. 单选:
  26. <input name="radio" type="radio" value="radio1" />radio1
  27. <input name="radio" type="radio" value="radio2" />radio2
  28. <br/>
  29. 多选:
  30. <input name="checkbox" type="checkbox" value="checkbox1" />checkbox1
  31. <input name="checkbox" type="checkbox" value="checkbox2" />checkbox2
  32. <input name="checkbox" type="checkbox" value="checkbox3" />checkbox3
  33. <br/>
  34. 下拉多选 :
  35. <select id="multiple" multiple="multiple" size="4">
  36. <option value="mul1">mul1</option>
  37. <option value="mul2">mul2</option>
  38. <option value="mul3">mul3</option>
  39. <option value="mul4">mul4</option>
  40. </select>
  41. <br/>
  42. 下拉单选 :
  43. <select id="single">
  44. <option value="sin1">sin1</option>
  45. <option value="sin2">sin2</option>
  46. <option value="sin3">sin3</option>
  47. </select>
  48. </body>
  49. </body>
  50. </html>

1.2. attr()、prop()

day04_jQuery学习笔记(下) - 图2

attr() 可以设置和获取属性的值,不推荐操作 checked、readOnly、selected、disabled 等等

  1. attr 方法还可以操作非标准的属性。比如自定义属性:abc,bbj

prop() 可以设置和获取属性的值,只推荐操作 checked、readOnly、selected、disabled 等等

  1. <!DOCTYPE html>
  2. <html lang="zh_CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script type="text/javascript" src="script/jquery-1.7.2.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. //attr
  10. // alert( $(":checkbox:first").attr("name") ); // 获取
  11. // $(":checkbox:first").attr("name","abc") ; // 设置
  12. // $(":checkbox").prop("checked",false );// 官方觉得返回undefined是一个错误
  13. // $(":checkbox:first").attr("abc","abcValue");
  14. // alert( $(":checkbox:first").attr("abc") );
  15. });
  16. </script>
  17. </head>
  18. <body>
  19. <br/>
  20. 多选:
  21. <input name="checkbox" type="checkbox" checked="checked" value="checkbox1" />checkbox1
  22. <input name="checkbox" type="checkbox" value="checkbox2" />checkbox2
  23. <br/><br/>
  24. <div>1234</div>
  25. <div>1234</div>
  26. </body>
  27. </html>

1.3. jQuery练习全选、全不选、反选

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. <script type="text/javascript" src="../../script/jquery-1.7.2.js"></script>
  7. <script type="text/javascript">
  8. $(function(){
  9. // 给全选绑定单击事件
  10. $("#checkedAllBtn").click(function () {
  11. $(":checkbox").prop("checked",true);
  12. });
  13. // 给全不选绑定单击事件
  14. $("#checkedNoBtn").click(function () {
  15. $(":checkbox").prop("checked",false);
  16. });
  17. // 反选单击事件
  18. $("#checkedRevBtn").click(function () {
  19. // 查询全部的球类的复选框
  20. $(":checkbox[name='items']").each(function () {
  21. // 在each遍历的function函数中,有一个this对象。这个this对象是当前正在遍历到的dom对象
  22. this.checked = !this.checked;
  23. });
  24. // 要检查 是否满选
  25. // 获取全部的球类个数
  26. var allCount = $(":checkbox[name='items']").length;
  27. // 再获取选中的球类个数
  28. var checkedCount = $(":checkbox[name='items']:checked").length;
  29. // if (allCount == checkedCount) {
  30. // $("#checkedAllBox").prop("checked",true);
  31. // } else {
  32. // $("#checkedAllBox").prop("checked",false);
  33. // }
  34. $("#checkedAllBox").prop("checked",allCount == checkedCount);
  35. });
  36. // 【提交】按钮单击事件
  37. $("#sendBtn").click(function () {
  38. // 获取选中的球类的复选框
  39. $(":checkbox[name='items']:checked").each(function () {
  40. alert(this.value);
  41. });
  42. });
  43. // 给【全选/全不选】绑定单击事件
  44. $("#checkedAllBox").click(function () {
  45. // 在事件的function函数中,有一个this对象,这个this对象是当前正在响应事件的dom对象
  46. // alert(this.checked);
  47. $(":checkbox[name='items']").prop("checked",this.checked);
  48. });
  49. // 给全部球类绑定单击事件
  50. $(":checkbox[name='items']").click(function () {
  51. // 要检查 是否满选
  52. // 获取全部的球类个数
  53. var allCount = $(":checkbox[name='items']").length;
  54. // 再获取选中的球类个数
  55. var checkedCount = $(":checkbox[name='items']:checked").length;
  56. $("#checkedAllBox").prop("checked",allCount == checkedCount);
  57. });
  58. });
  59. </script>
  60. </head>
  61. <body>
  62. <form method="post" action="">
  63. 你爱好的运动是?<input type="checkbox" id="checkedAllBox" />全选/全不选
  64. <br />
  65. <input type="checkbox" name="items" value="足球" />足球
  66. <input type="checkbox" name="items" value="篮球" />篮球
  67. <input type="checkbox" name="items" value="羽毛球" />羽毛球
  68. <input type="checkbox" name="items" value="乒乓球" />乒乓球
  69. <br />
  70. <input type="button" id="checkedAllBtn" value="全 选" />
  71. <input type="button" id="checkedNoBtn" value="全不选" />
  72. <input type="button" id="checkedRevBtn" value="反 选" />
  73. <input type="button" id="sendBtn" value="提 交" />
  74. </form>
  75. </body>
  76. </html>

2. DOM的增删改

day04_jQuery学习笔记(下) - 图3

内部插入:

appendTo() a.appendTo(b) 把 a 插入到 b 子元素末尾,成为最后一个子元素

prependTo() a.prependTo(b) 把 a 插到 b 所有子元素前面,成为第一个子元素

外部插入:

insertAfter() a.insertAfter(b) 得到 ba

insertBefore() a.insertBefore(b) 得到 ab

替换:

replaceWith() a.replaceWith(b) 用 b 替换掉 a

replaceAll() a.replaceAll(b) 用 a 替换掉所有 b

删除:

remove() a.remove(); 删除 a 标签

empty() a.empty(); 清空 a 标签里的内容

  1. <!DOCTYPE html>
  2. <html lang="zh_CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script type="text/javascript" src="script/jquery-1.7.2.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. // $("<h1>标题</h1>").prependTo( $("div") );
  10. // $("<h1>标题</h1>").prependTo("div");
  11. // $("<h1>标题</h1>").insertAfter("div");
  12. // $("<h1>标题</h1>").insertBefore( $("div") );
  13. // $("<h1>标题</h1>").replaceWith("div");
  14. // $("div").replaceWith( $("<h1>标题</h1>") );
  15. // $("<h1>标题</h1>").replaceAll( "div" );
  16. $("div").empty();
  17. });
  18. </script>
  19. </head>
  20. <body>
  21. <body>
  22. <br/>
  23. 多选:
  24. <input name="checkbox" type="checkbox" checked="checked" value="checkbox1" />checkbox1
  25. <input name="checkbox" type="checkbox" value="checkbox2" />checkbox2
  26. <br/><br/>
  27. <div>1234</div>
  28. <div>1234</div>
  29. </body>
  30. </body>
  31. </html>

2.1. jQuery练习:从左到右,从右到左

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. <style type="text/css">
  7. select {
  8. width: 100px;
  9. height: 140px;
  10. }
  11. div {
  12. width: 130px;
  13. float: left;
  14. text-align: center;
  15. }
  16. </style>
  17. <script type="text/javascript" src="script/jquery-1.7.2.js"></script>
  18. <script type="text/javascript">
  19. // 页面加载完成
  20. $(function () {
  21. // 第一个按钮 【选中添加到右边】
  22. $("button:eq(0)").click(function () {
  23. $("select:eq(0) option:selected").appendTo($("select:eq(1)"));
  24. });
  25. // 第二个按钮 【全部添加到右边】
  26. $("button:eq(1)").click(function () {
  27. $("select:eq(0) option").appendTo($("select:eq(1)"));
  28. });
  29. // 第三个按钮 【选中删除到左边】
  30. $("button:eq(2)").click(function () {
  31. $("select:eq(1) option:selected").appendTo($("select:eq(0)"));
  32. });
  33. // 第四个按钮 【全部删除到左边】
  34. $("button:eq(3)").click(function () {
  35. $("select:eq(1) option").appendTo($("select:eq(0)"));
  36. });
  37. });
  38. </script>
  39. </head>
  40. <body>
  41. <div id="left">
  42. <select multiple="multiple" name="sel01">
  43. <option value="opt01">选项1</option>
  44. <option value="opt02">选项2</option>
  45. <option value="opt03">选项3</option>
  46. <option value="opt04">选项4</option>
  47. <option value="opt05">选项5</option>
  48. <option value="opt06">选项6</option>
  49. <option value="opt07">选项7</option>
  50. <option value="opt08">选项8</option>
  51. </select>
  52. <button>选中添加到右边</button>
  53. <button>全部添加到右边</button>
  54. </div>
  55. <div id="rigth">
  56. <select multiple="multiple" name="sel02">
  57. </select>
  58. <button>选中删除到左边</button>
  59. <button>全部删除到左边</button>
  60. </div>
  61. </body>
  62. </html>

2.2. jQuery练习:动态添加和删除行记录

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Untitled Document</title>
  6. <link rel="stylesheet" type="text/css" href="styleB/css.css" />
  7. <script type="text/javascript" src="../../script/jquery-1.7.2.js"></script>
  8. <script type="text/javascript">
  9. $(function () {
  10. // 创建一个用于复用的删除的function函数
  11. var deleteFun = function(){
  12. // alert("删除 操作 的function : " + this);
  13. // 在事件响应的function函数中,有一个this对象。这个this对象是当前正在响应事件的dom对象。
  14. var $trObj = $(this).parent().parent();
  15. var name = $trObj.find("td:first").text();
  16. /**
  17. * confirm 是JavaScript语言提供的一个确认提示框函数。你给它传什么,它就提示什么<br/>
  18. * 当用户点击了确定,就返回true。当用户点击了取消,就返回false
  19. */
  20. if( confirm("你确定要删除[" + name + "]吗?") ){
  21. $trObj.remove();
  22. }
  23. // return false; 可以阻止 元素的默认行为。
  24. return false;
  25. };
  26. // 给【Submit】按钮绑定单击事件
  27. $("#addEmpButton").click(function () {
  28. // 获取输入框,姓名,邮箱,工资的内容
  29. var name = $("#empName").val();
  30. var email = $("#email").val();
  31. var salary = $("#salary").val();
  32. // 创建一个行标签对象,添加到显示数据的表格中
  33. var $trObj = $("<tr>" +
  34. "<td>" + name + "</td>" +
  35. "<td>" + email + "</td>" +
  36. "<td>" + salary + "</td>" +
  37. "<td><a href=\"deleteEmp?id=002\">Delete</a></td>" +
  38. "</tr>");
  39. // 添加到显示数据的表格中
  40. $trObj.appendTo( $("#employeeTable") );
  41. // 给添加的行的a标签绑上事件
  42. $trObj.find("a").click( deleteFun );
  43. });
  44. // 给删除的a标签绑定单击事件
  45. $("a").click( deleteFun );
  46. });
  47. </script>
  48. </head>
  49. <body>
  50. <table id="employeeTable">
  51. <tr>
  52. <th>Name</th>
  53. <th>Email</th>
  54. <th>Salary</th>
  55. <th>&nbsp;</th>
  56. </tr>
  57. <tr>
  58. <td>Tom</td>
  59. <td>tom@tom.com</td>
  60. <td>5000</td>
  61. <td><a href="deleteEmp?id=001">Delete</a></td>
  62. </tr>
  63. <tr>
  64. <td>Jerry</td>
  65. <td>jerry@sohu.com</td>
  66. <td>8000</td>
  67. <td><a href="deleteEmp?id=002">Delete</a></td>
  68. </tr>
  69. <tr>
  70. <td>Bob</td>
  71. <td>bob@tom.com</td>
  72. <td>10000</td>
  73. <td><a href="deleteEmp?id=003">Delete</a></td>
  74. </tr>
  75. </table>
  76. <div id="formDiv">
  77. <h4>添加新员工</h4>
  78. <table>
  79. <tr>
  80. <td class="word">name: </td>
  81. <td class="inp">
  82. <input type="text" name="empName" id="empName" />
  83. </td>
  84. </tr>
  85. <tr>
  86. <td class="word">email: </td>
  87. <td class="inp">
  88. <input type="text" name="email" id="email" />
  89. </td>
  90. </tr>
  91. <tr>
  92. <td class="word">salary: </td>
  93. <td class="inp">
  94. <input type="text" name="salary" id="salary" />
  95. </td>
  96. </tr>
  97. <tr>
  98. <td colspan="2" align="center">
  99. <button id="addEmpButton" value="abc">
  100. Submit
  101. </button>
  102. </td>
  103. </tr>
  104. </table>
  105. </div>
  106. </body>
  107. </html>

3. jQuery的css样式操作

addClass() 添加样式

removeClass() 删除样式

toggleClass() 有就删除,没有就添加样式。

offset() 获取和设置元素的坐标。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. <style type="text/css">
  7. div{
  8. width:100px;
  9. height:260px;
  10. }
  11. div.whiteborder{
  12. border: 2px white solid;
  13. }
  14. div.redDiv{
  15. background-color: red;
  16. }
  17. div.blueBorder{
  18. border: 5px blue solid;
  19. }
  20. </style>
  21. <script type="text/javascript" src="script/jquery-1.7.2.js"></script>
  22. <script type="text/javascript">
  23. $(function(){
  24. var $divEle = $('div:first');
  25. $('#btn01').click(function(){
  26. //addClass() - 向被选元素添加一个或多个类
  27. $divEle.addClass('redDiv blueBorder');
  28. });
  29. $('#btn02').click(function(){
  30. //removeClass() - 从被选元素删除一个或多个类
  31. $divEle.removeClass();
  32. });
  33. $('#btn03').click(function(){
  34. //toggleClass() - 对被选元素进行添加/删除类的切换操作
  35. $divEle.toggleClass('redDiv')
  36. });
  37. $('#btn04').click(function(){
  38. //offset() - 返回第一个匹配元素相对于文档的位置。
  39. var pos = $divEle.offset();
  40. console.log(pos);
  41. $divEle.offset({
  42. top:100,
  43. left:50
  44. });
  45. });
  46. })
  47. </script>
  48. </head>
  49. <body>
  50. <table align="center">
  51. <tr>
  52. <td>
  53. <div class="border">
  54. </div>
  55. </td>
  56. <td>
  57. <div class="btn">
  58. <input type="button" value="addClass()" id="btn01"/>
  59. <input type="button" value="removeClass()" id="btn02"/>
  60. <input type="button" value="toggleClass()" id="btn03"/>
  61. <input type="button" value="offset()" id="btn04"/>
  62. </div>
  63. </td>
  64. </tr>
  65. </table>
  66. <br /> <br />
  67. <br /> <br />
  68. </body>
  69. </html>

4. jQuery的动画操作

基本动画

show() 将隐藏的元素显示

hide() 将可见的元素隐藏。

toggle() 可见就隐藏,不可见就显示。

以上动画方法都可以添加参数。

  1. 第一个参数是动画 执行的时长,以毫秒为单位

  2. 第二个参数是动画的回调函数 (动画完成后自动调用的函数)

淡入淡出动画

fadeIn() 淡入(慢慢可见)

fadeOut() 淡出(慢慢消失)

fadeTo() 在指定时长内慢慢的将透明度修改到指定的值。0 透明,1 完成可见,0.5 半透明

fadeToggle() 淡入/淡出 切换

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Untitled Document</title>
  6. <link href="css/style.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="script/jquery-1.7.2.js"></script>
  8. <script type="text/javascript">
  9. /*
  10. 基本
  11. show([speed,[easing],[fn]])
  12. hide([speed,[easing],[fn]])
  13. toggle([speed],[easing],[fn])
  14. 滑动
  15. slideDown([spe],[eas],[fn])
  16. slideUp([speed,[easing],[fn]])
  17. slideToggle([speed],[easing],[fn])
  18. 淡入淡出
  19. fadeIn([speed],[eas],[fn])
  20. fadeOut([speed],[eas],[fn])
  21. fadeTo([[spe],opa,[eas],[fn]])
  22. fadeToggle([speed,[eas],[fn]])
  23. */
  24. $(function(){
  25. //显示 show()
  26. $("#btn1").click(function(){
  27. $("#div1").show(2000,function () {
  28. alert("show动画完成 ")
  29. });
  30. });
  31. //隐藏 hide()
  32. $("#btn2").click(function(){
  33. $("#div1").hide(1000,function () {
  34. alert("hide动画 执行完成 ")
  35. });
  36. });
  37. //切换 toggle()
  38. $("#btn3").click(function(){
  39. $("#div1").toggle(1000,function () {
  40. alert("toggle动画 完成 ")
  41. });
  42. });
  43. // var abc = function(){
  44. // $("#div1").toggle(1000,abc);
  45. // }
  46. // abc();
  47. //淡入 fadeIn()
  48. $("#btn4").click(function(){
  49. $("#div1").fadeIn(2000,function () {
  50. alert("fadeIn完成 ")
  51. });
  52. });
  53. //淡出 fadeOut()
  54. $("#btn5").click(function(){
  55. $("#div1").fadeOut(2000,function () {
  56. alert("fadeOut完成 ")
  57. });
  58. });
  59. //淡化到 fadeTo()
  60. $("#btn6").click(function(){
  61. $("#div1").fadeTo(2000,0.5,function () {
  62. alert('fadeTo完成 ')
  63. });
  64. });
  65. //淡化切换 fadeToggle()
  66. $("#btn7").click(function(){
  67. $("#div1").fadeToggle(1000,function () {
  68. alert("fadeToggle完成 ")
  69. });
  70. });
  71. })
  72. </script>
  73. </head>
  74. <body>
  75. <table style="float: left;">
  76. <tr>
  77. <td><button id="btn1">显示show()</button></td>
  78. </tr>
  79. <tr>
  80. <td><button id="btn2">隐藏hide()</button></td>
  81. </tr>
  82. <tr>
  83. <td><button id="btn3">显示/隐藏切换 toggle()</button></td>
  84. </tr>
  85. <tr>
  86. <td><button id="btn4">淡入fadeIn()</button></td>
  87. </tr>
  88. <tr>
  89. <td><button id="btn5">淡出fadeOut()</button></td>
  90. </tr>
  91. <tr>
  92. <td><button id="btn6">淡化到fadeTo()</button></td>
  93. </tr>
  94. <tr>
  95. <td><button id="btn7">淡化切换fadeToggle()</button></td>
  96. </tr>
  97. </table>
  98. <div id="div1" style="float:left;border: 1px solid;background-color: blue;width: 300px;height: 200px;">
  99. jquery动画定义了很多种动画效果,可以很方便的使用这些动画效果
  100. </div>
  101. </body>
  102. </html>

4.1. jQuery练习:品牌展示

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  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. <style type="text/css">
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. body {
  12. font-size: 12px;
  13. text-align: center;
  14. }
  15. a {
  16. color: #04D;
  17. text-decoration: none;
  18. }
  19. a:hover {
  20. color: #F50;
  21. text-decoration: underline;
  22. }
  23. .SubCategoryBox {
  24. width: 600px;
  25. margin: 0 auto;
  26. text-align: center;
  27. margin-top: 40px;
  28. }
  29. .SubCategoryBox ul {
  30. list-style: none;
  31. }
  32. .SubCategoryBox ul li {
  33. display: block;
  34. float: left;
  35. width: 200px;
  36. line-height: 20px;
  37. }
  38. .showmore , .showless{
  39. clear: both;
  40. text-align: center;
  41. padding-top: 10px;
  42. }
  43. .showmore a , .showless a{
  44. display: block;
  45. width: 120px;
  46. margin: 0 auto;
  47. line-height: 24px;
  48. border: 1px solid #AAA;
  49. }
  50. .showmore a span {
  51. padding-left: 15px;
  52. background: url(img/down.gif) no-repeat 0 0;
  53. }
  54. .showless a span {
  55. padding-left: 15px;
  56. background: url(img/up.gif) no-repeat 0 0;
  57. }
  58. .promoted a {
  59. color: #F50;
  60. }
  61. </style>
  62. <script type="text/javascript" src="script/jquery-1.7.2.js"></script>
  63. <script type="text/javascript">
  64. $(function() {
  65. // 基本初始状态
  66. $("li:gt(5):not(:last)").hide();
  67. // 给功能的按钮绑定单击事件
  68. $("div div a").click(function () {
  69. // 让某些品牌,显示,或隐藏
  70. $("li:gt(5):not(:last)").toggle();
  71. // 判断 品牌,当前是否可见
  72. if( $("li:gt(5):not(:last)").is(":hidden") ){
  73. // 品牌隐藏的状态 :1 显示全部品牌 == 角标向下 showmore
  74. $("div div a span").text("显示全部品牌");
  75. $("div div").removeClass();
  76. $("div div").addClass("showmore");
  77. // 去掉高亮
  78. $("li:contains('索尼')").removeClass("promoted");
  79. } else {
  80. // 品牌可见的状态:2 显示精简品牌 == 角标向上 showless
  81. $("div div a span").text("显示精简品牌");
  82. $("div div").removeClass();
  83. $("div div").addClass("showless");
  84. // 加高亮
  85. $("li:contains('索尼')").addClass("promoted");
  86. }
  87. return false;
  88. });
  89. });
  90. </script>
  91. </head>
  92. <body>
  93. <div class="SubCategoryBox">
  94. <ul>
  95. <li><a href="#">佳能</a><i>(30440) </i></li>
  96. <li><a href="#">索尼</a><i>(27220) </i></li>
  97. <li><a href="#">三星</a><i>(20808) </i></li>
  98. <li><a href="#">尼康</a><i>(17821) </i></li>
  99. <li><a href="#">松下</a><i>(12289) </i></li>
  100. <li><a href="#">卡西欧</a><i>(8242) </i></li>
  101. <li><a href="#">富士</a><i>(14894) </i></li>
  102. <li><a href="#">柯达</a><i>(9520) </i></li>
  103. <li><a href="#">宾得</a><i>(2195) </i></li>
  104. <li><a href="#">理光</a><i>(4114) </i></li>
  105. <li><a href="#">奥林巴斯</a><i>(12205) </i></li>
  106. <li><a href="#">明基</a><i>(1466) </i></li>
  107. <li><a href="#">爱国者</a><i>(3091) </i></li>
  108. <li><a href="#">其它品牌相机</a><i>(7275) </i></li>
  109. </ul>
  110. <div class="showmore">
  111. <a href="more.html"><span>显示全部品牌</span></a>
  112. </div>
  113. </div>
  114. </body>
  115. </html>

5. jQuery事件操作

5.1. $( function(){} ); 和 window.onload = function(){} 的区别?

他们分别是在什么时候触发?

  1. jQuery 的页面加载完成之后是浏览器的内核解析完页面的标签创建好 DOM 对象之后就会马上执行。

  2. 原生 js 的页面加载完成之后,除了要等浏览器内核解析完标签创建好 DOM 对象,还要等标签显示时需要的内容加载 完成。

他们触发的顺序?

  1. jQuery 页面加载完成之后先执行

  2. 原生 js 的页面加载完成之后

他们执行的次数?

  1. 原生 js 的页面加载完成之后,只会执行最后一次的赋值函数。

  2. jQuery 的页面加载完成之后是全部把注册的 function 函数,依次顺序全部执行。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. <script type="text/javascript" src="../../script/jquery-1.7.2.js"></script>
  7. <script type="text/javascript">
  8. window.onload = function () {
  9. alert("原生js的页面加载完成之后--1")
  10. }
  11. window.onload = function () {
  12. alert("原生js的页面加载完成之后--2")
  13. }
  14. window.onload = function () {
  15. alert("原生js的页面加载完成之后--3")
  16. }
  17. $(function () {
  18. alert("jquery的页面加载完成 之后--3")
  19. });
  20. // jquery的页面加载完成 之后
  21. $(function () {
  22. alert("jquery的页面加载完成 之后--1")
  23. });
  24. $(function () {
  25. alert("jquery的页面加载完成 之后--2")
  26. });
  27. </script>
  28. </head>
  29. <body>
  30. <button>我是按钮</button>
  31. <iframe src="http://localhost:8080"></iframe>
  32. <img src="http://localhost:8080/1.jpg" alt=""/>
  33. </body>
  34. </html>

5.2. jQuery中其他的事件处理方法

click() 它可以绑定单击事件,以及触发单击事件

mouseover() 鼠标移入事件

mouseout() 鼠标移出事件

bind() 可以给元素一次性绑定一个或多个事件。

one() 使用上跟 bind 一样。但是 one 方法绑定的事件只会响应一次。

unbind() 跟 bind 方法相反的操作,解除事件的绑定

live() 也是用来绑定事件。它可以用来绑定选择器匹配的所有元素的事件。哪怕这个元素是后面动态创建出 来的也有效

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Untitled Document</title>
  6. <link href="css/style.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../../script/jquery-1.7.2.js"></script>
  8. <script type="text/javascript">
  9. $(function(){
  10. // $("h5").click(function () { // 传function是绑定事件
  11. // alert('h5单击事件 == click方法绑定')
  12. // });
  13. // 使用live绑定的单击事件
  14. $("h5").live("click",function () {
  15. alert('h5单击事件 == live方法绑定');
  16. });
  17. $('<h5 class="head">什么是jQuery?</h5>').appendTo( $("#panel") );
  18. // $("button").click(function () {
  19. // $("h5").click(); // 不传function是触发事件
  20. // });
  21. //鼠标移入
  22. // $("h5").mouseover(function () {
  23. // console.log("你进来了")
  24. // });
  25. // //鼠标移出
  26. // $("h5").mouseout(function () {
  27. // console.log("你出去了")
  28. // });
  29. // 使用bind绑定事件
  30. // $("h5").bind("click mouseover mouseout",function () {
  31. // console.log("这是bind绑定的事件");
  32. // });
  33. // $("h5").one("click mouseover mouseout",function () {
  34. // console.log("这是one绑定的事件");
  35. // });
  36. // $("h5").unbind();
  37. });
  38. </script>
  39. </head>
  40. <body>
  41. <div id="panel">
  42. <h5 class="head">什么是jQuery?</h5>
  43. <div class="content">
  44. jQuery是继Prototype之后又一个优秀的JavaScript库,它是一个由 John Resig 创建于2006年1月的开源项目。jQuery凭借简洁的语法和跨平台的兼容性,极大地简化了JavaScript开发人员遍历HTML文档、操作DOM、处理事件、执行动画和开发Ajax。它独特而又优雅的代码风格改变了JavaScript程序员的设计思路和编写程序的方式。
  45. </div>
  46. <button>按钮</button>
  47. </div>
  48. </body>
  49. </html>

5.3. 事件的冒泡

什么是事件的冒泡?

事件的冒泡是指,父子元素同时监听同一个事件。当触发子元素的事件的时候,同一个事件也被传递到了父元素的事件里去 响应。

那么如何阻止事件冒泡呢?

在子元素事件函数体内,return false; 可以阻止事件的冒泡传递。

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Untitled Document</title>
  6. <style type="text/css">
  7. *{
  8. margin: 0;
  9. padding: 0;
  10. }
  11. body{
  12. font-size: 13px;
  13. line-height: 130%;
  14. padding: 60px;
  15. }
  16. #content{
  17. width: 220px;
  18. border: 1px solid #0050D0;
  19. background: #96E555;
  20. }
  21. span{
  22. width: 200px;
  23. margin: 10px;
  24. background: #666666;
  25. cursor: pointer;
  26. color: white;
  27. display: block;
  28. }
  29. p{
  30. width: 200px;
  31. background: #888;
  32. color: white;
  33. height: 16px;
  34. }
  35. </style>
  36. <script type="text/javascript" src="jquery-1.7.2.js"></script>
  37. <script type="text/javascript">
  38. $(function(){
  39. $("#content").click(function () {
  40. alert('我是div');
  41. });
  42. $("span").click(function () {
  43. alert('我是span');
  44. return false;
  45. });
  46. })
  47. </script>
  48. </head>
  49. <body>
  50. <div id="content">
  51. 外层div元素
  52. <span>内层span元素</span>
  53. 外层div元素
  54. </div>
  55. <div id="msg"></div>
  56. <br><br>
  57. <a href="http://www.hao123.com">WWW.HAO123.COM</a>
  58. </body>
  59. </html>

5.4. 事件对象

事件对象,是封装有触发的事件信息的一个 javascript 对象。

我们重点关心的是怎么拿到这个 javascript 的事件对象。以及使用。

如何获取呢 javascript 事件对象呢?

在给元素绑定事件的时候,在事件的 function( event ) 参数列表中添加一个参数,这个参数名,我们习惯取名为 event。 这个 event 就是 javascript 传递参事件处理函数的事件对象。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. <style type="text/css">
  7. #areaDiv {
  8. border: 1px solid black;
  9. width: 300px;
  10. height: 50px;
  11. margin-bottom: 10px;
  12. }
  13. #showMsg {
  14. border: 1px solid black;
  15. width: 300px;
  16. height: 20px;
  17. }
  18. </style>
  19. <script type="text/javascript" src="jquery-1.7.2.js"></script>
  20. <script type="text/javascript">
  21. //1.原生javascript获取 事件对象
  22. // window.onload = function () {
  23. // document.getElementById("areaDiv").onclick = function (event) {
  24. // console.log(event);
  25. // }
  26. // }
  27. //2.JQuery代码获取 事件对象
  28. $(function () {
  29. // $("#areaDiv").click(function (event) {
  30. // console.log(event);
  31. // });
  32. //3.使用bind同时对多个事件绑定同一个函数。怎么获取当前操作是什么事件。
  33. $("#areaDiv").bind("mouseover mouseout",function (event) {
  34. if (event.type == "mouseover") {
  35. console.log("鼠标移入");
  36. } else if (event.type == "mouseout") {
  37. console.log("鼠标移出");
  38. }
  39. });
  40. });
  41. </script>
  42. </head>
  43. <body>
  44. <div id="areaDiv"></div>
  45. <div id="showMsg"></div>
  46. </body>
  47. </html>

5.5. jQuery练习:图片跟随

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. <style type="text/css">
  7. body {
  8. text-align: center;
  9. }
  10. #small {
  11. margin-top: 150px;
  12. }
  13. #showBig {
  14. position: absolute;
  15. display: none;
  16. }
  17. </style>
  18. <script type="text/javascript" src="script/jquery-1.7.2.js"></script>
  19. <script type="text/javascript">
  20. $(function(){
  21. $("#small").bind("mouseover mouseout mousemove",function (event) {
  22. if (event.type == "mouseover") {
  23. $("#showBig").show();
  24. } else if (event.type == "mousemove") {
  25. console.log(event);
  26. $("#showBig").offset({
  27. left: event.pageX + 10,
  28. top: event.pageY + 10
  29. });
  30. } else if (event.type == "mouseout") {
  31. $("#showBig").hide();
  32. }
  33. });
  34. });
  35. </script>
  36. </head>
  37. <body>
  38. <img id="small" src="img/small.jpg" />
  39. <div id="showBig">
  40. <img src="img/big.jpg">
  41. </div>
  42. </body>
  43. </html>

6. 书城项目第一阶段,表单验证

验证用户名:必须由字母,数字下划线组成,并且长度为 5 到 12 位

验证密码:必须由字母,数字下划线组成,并且长度为 5 到 12 位

验证确认密码:和密码相同

邮箱验证:xxxxx@xxx.com

验证码:现在只需要验证用户已输入。因为还没讲到服务器。验证码生成。

  1. <script type="text/javascript">
  2. // 页面加载完成之后
  3. $(function () {
  4. // 给注册绑定单击事件
  5. $("#sub_btn").click(function () {
  6. // 验证用户名:必须由字母,数字下划线组成,并且长度为 5 到 12 位
  7. //1 获取用户名输入框里的内容
  8. var usernameText = $("#username").val();
  9. //2 创建正则表达式对象
  10. var usernamePatt = /^\w{5,12}$/;
  11. //3 使用 test 方法验证
  12. if (!usernamePatt.test(usernameText)) {
  13. //4 提示用户结果
  14. $("span.errorMsg").text("用户名不合法!");
  15. return false;
  16. }
  17. // 验证密码:必须由字母,数字下划线组成,并且长度为 5 到 12 位
  18. //1 获取用户名输入框里的内容
  19. var passwordText = $("#password").val();
  20. //2 创建正则表达式对象
  21. var passwordPatt = /^\w{5,12}$/;
  22. //3 使用 test 方法验证
  23. if (!passwordPatt.test(passwordText)) {
  24. //4 提示用户结果
  25. $("span.errorMsg").text("密码不合法!");
  26. return false;
  27. }
  28. // 验证确认密码:和密码相同
  29. //1 获取确认密码内容
  30. var repwdText = $("#repwd").val();
  31. //2 和密码相比较
  32. if (repwdText != passwordText) {
  33. //3 提示用户
  34. $("span.errorMsg").text("确认密码和密码不一致!");
  35. return false;
  36. }
  37. // 邮箱验证:xxxxx@xxx.com
  38. //1 获取邮箱里的内容
  39. var emailText = $("#email").val();
  40. //2 创建正则表达式对象
  41. var emailPatt = /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/;
  42. //3 使用 test 方法验证是否合法
  43. if (!emailPatt.test(emailText)) {
  44. //4 提示用户
  45. $("span.errorMsg").text("邮箱格式不合法!");
  46. return false;
  47. }
  48. // 验证码:现在只需要验证用户已输入。因为还没讲到服务器。验证码生成。
  49. var codeText = $("#code").val();
  50. //去掉验证码前后空格
  51. alert("去空格前:["+codeText+"]")
  52. codeText = $.trim(codeText);
  53. alert("去空格后:["+codeText+"]")
  54. if (codeText == null || codeText == "") {
  55. //4 提示用户
  56. $("span.errorMsg").text("验证码不能为空!");
  57. return false;
  58. }
  59. $("span.errorMsg").text("");
  60. });
  61. });