经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jQuery基础系列
来源:cnblogs  作者:达叔小生  时间:2019/4/23 8:48:32  对本文有异议

jQuery基础系列

image.png

  1. $(document).ready(function(){
  2. $("p").click(function(){
  3. $(this).hide();
  4. });
  5. });

image.png

image.png

  1. jQuery 入口函数:
  2. $(document).ready(function(){
  3. // 执行代码
  4. });
  5. 或者
  6. $(function(){
  7. // 执行代码
  8. });
  9. JavaScript 入口函数:
  10. window.onload = function () {
  11. // 执行代码
  12. }

jQuery 入口函数与 JavaScript 入口函数的区别:

jquery的入口函数是在html所有标签都加载后才执行,而JavaScript的window.onload事件是等到所有内容加载完后才执行。

image.png

  1. $(document).ready(function(){
  2. $("button").click(function(){
  3. $("p").hide();
  4. });
  5. });
  6. $(document).ready(function(){
  7. $("button").click(function(){
  8. $("#test").hide();
  9. });
  10. });
  11. $(document).ready(function(){
  12. $("button").click(function(){
  13. $(".test").hide();
  14. });
  15. });
  1. <script>
  2. $(function(){
  3. var DOMObject = document.getElementById('div');
  4. var jQueryObject = $('#div');
  5. console.log(DOMObject);
  6. console.log(jQueryObject);
  7. })
  8. </script>
  9. $("a[target='_blank']")
  10. $("a[target!='_blank']")
  11. $("tr:even") 选取偶数位置的 <tr> 元素
  12. $("tr:odd") 选取奇数位置的 <tr> 元素

什么是事件?
页面的响应叫做事件

  1. dom事件:
  2. click dbclick mouseenter mouseleave
  3. keypress keydown keyup hover
  4. submit change focus blur
  5. load resize scroll unload

image.png

  1. var jqueryObj = $(domObj);
  2. var domObj = jqueryObj.get([index]);
  3. console.log(DOMObject.jquery);
  4. console.log(jQueryObject.nodeType);
  5. <script>
  6. $(function(){
  7. var div = $('<div>hello</div>');
  8. console.log(div);
  9. console.log($('div'));
  10. })
  11. </script>
  12. <script>
  13. $(function(){
  14. var link1 = $('<a>', {
  15. text: 'baidu',
  16. href: 'http://www.baidu.com',
  17. target: '_blank',
  18. title: 'go'
  19. });
  20. link1.appendTo('body');
  21. var link2 = $('<a>baidu</a>').attr({
  22. text: 'baidu',
  23. href: 'http://www.baidu.com',
  24. target: '_blank',
  25. title: 'go'
  26. })
  27. });
  28. </script>

image.png

  1. <script>
  2. $(function(){
  3. var elements = $('li');
  4. console.log(elements.length);
  5. console.log(elements.get());
  6. console.log(elements[0]);
  7. })
  8. </script>

index返回dom元素
get([index]))返回dom元素或元素的集合
eq(index)返回jquery对象

  1. children([selector])
  2. contents()
  3. find(selector)
  4. parent([selector])
  5. parents([selector])
  6. parentsUntil([selector])
  7. closest(selector)
  1. <scripts>
  2. $(function(){
  3. var elements = $('li');
  4. console.log(elements.eq(0));
  5. console.log($('li:eq(0)'));
  6. ))
  7. </script>

first()和last()和toArray()

操作元素的特性,属性,和数据

  1. 获取特性的值:attr(name)
  2. 设置特性的值:attr(name,value) attr(attributes)

添加类:addClass(name)
removeClass(names)
hasClass(name)
toggleClass(names][,switch])

jquery是一款JavaScript库,jQuery可以处理HTML,事件,动画等。jQuery是可以兼容多个浏览器,下载jQuery。

  1. http://jquery.com/
  1. write less, do more

html表现结构,js表示行为,css表示为表现。

  1. window.jQuery === window.$
  1. $.each()
  2. $.noop
  3. $.toArray()
  1. text('hello')
  2. removeClass('blue')
  3. addClass('blue')
  4. css('color','red')
  1. $('$div').text('hello').removeClass('blue').addClass('bold').css('color','red');
  1. $(document).ready(function(){...})
  1. document.getElementById();
  2. document.getElementsByName();
  3. document.getElementsByTagName();
  4. document.getElementsByClassName();

选择JavaScript中的元素:

  1. document.querySelector();
  2. document.querySelectorAll();

css选择器:基本选择器,属性选择器,伪类选择器,伪元素选择器。

  1. * 通配符选择器
  2. E 元素选择器
  3. .class 类选择器
  4. #id id选择器
  5. E F 后代选择器
  6. E + F 相邻兄弟选择器
  7. E ~ F 通用兄弟元素选择器
  8. E[attr] 只使用属性名
  9. :link 选择所有未被访问的连接
  10. :visited 选择所有已被访问的链接
  11. :hover 鼠标指针到其上的链接
  12. :active 选择活动链接
  13. :focus 选择获得焦点的input元素
  14. :enabled 选择每个启用的input元素
  15. :disabled 选择每个禁用的input元素
  16. :checked 选择每个选中的input元素
  17. :first-child 选择某个元素的第一个元素
  18. :last-child 选择某个元素的最后一个子元素
  19. :nth-child() 1开始的元素,选择某个元素
  20. :nth-last-child() 选择某个元素的一个或多个特定的元素
  21. :nth-of-type() 类似 :nth-child,只有符合type类型
  22. :nth-last-of-type() nth-last-child() 类似,从最后一个子元素开始算
  23. :first-of-type 选择一个上级元素的第一个同类子元素
  24. :last-of-type 选择一个上级元素的最后一个同类子元素
  25. :empty 选择的元素里面没有任何内容,这里没有内容指的是一点内容都没有
  26. :not() 否定选择器
  27. :first-line 用于选取指定选择器的首行
  28. :first-letter 用于选取首字母
  29. ::before 在被选元素的内容前面插入内容
  30. ::after 在被选元素的内容后面插入内容
  31. ::selection 应用于文档中被用户高亮的部分

jQuery中的基本选择器:id选择器,类选择器,元素选择器,后代选择器,属性选择器。

位置筛选器,子元素筛选器,表单筛选器,内容筛选器,自定义筛选器,其他筛选器。

位置筛选器:

  1. :first
  2. :last
  3. :even
  4. :odd
  5. :eq(n)
  6. :gt(n)
  7. :lt(n)

子元素的筛选器:

  1. :first-child
  2. :last-child
  3. :first-of-type
  4. :last-of-type
  5. :nth-child()
  6. :nth-last-child()
  7. :nth-of-type()
  8. :nth-last-of-type()
  9. :only-child
  10. :only-of-type

表单筛选器:

  1. :checked
  2. :disabled
  3. :enabled
  4. :focus
  5. :button
  6. :checkbox
  7. :file
  8. :image
  9. :input
  10. :password
  11. :radio
  12. :reset
  13. :selected
  14. :submit
  15. :text

内容筛选器

  1. :empty
  2. :contains(text)
  3. :has(selector)
  4. :parent

筛选器

  1. :lang()
  2. :not()
  3. :root
  4. :target
  5. :hidden
  6. :header
  7. :animated

dom对象和jQuery对象:

  1. obj.nodeType
  2. obj.jquery
  3. var jqueryObj = $(domObj);
  4. var domObj = jqueryObj.get([index]);
  1. children([selector])
  2. contents()
  3. find(selector)
  4. parent([selector])
  5. parentsUntil([selector])
  6. closest(selector)
  7. next([selector])
  8. nextAll([selector])
  9. nextUntil([selector])
  10. prev([selector])
  11. prevAll([selector])
  12. prevUntil([selector])
  13. siblings([selector])
  14. add(selector)
  15. not(selector)
  16. filter(selector)
  17. has(selector)
  18. slice(start[,end])
  19. map(callback)
  20. each(iterator)
  21. is(selector)
  22. end()
  23. addBack()
  24. html()
  25. html(content)
  26. text()
  27. text(content)
  1. append
  2. prepend
  3. before
  4. after
  5. appendTo
  6. prependTo
  7. insertBefore
  8. insertAfter
  9. wrap()
  10. wrapAll()
  11. wrapInner()
  12. unwrap()
  13. remove()
  14. datach()
  15. empty()
  16. clone()
  17. replaceWith()
  18. replaceAll()
  19. val()

jQuery常用的函数:

  1. .get() 获取指定的dom元素
  2. .index() 返回指定元素相对于其他指定元素的index位置
  3. .size() 返回被jQuery选择器匹配的元素的数量
  4. .toArray() 以数组的形式返回jQuery选择器匹配的元素
  1. .add() 将元素添加到匹配元素的集合中
  2. .addSelf() 把堆栈中之前的元素添加到当前集合中
  3. .children() 获取匹配元素集合中每个元素的所有子元素
  4. .closest() 从元素本身开始,逐级向上元素匹配,并返回最先匹配的祖先元素
  5. .contents() 获得匹配元素集合中每个元素的子元素
  6. .each() jQuery对象进行迭代,为每个匹配元素执行函数
  7. .end() 结束当前链中最近的一次筛选操作,并将匹配元素集合返回到前一次的状态
  8. .eq() 将匹配元素集合缩减为位于索引的新元素
  9. .filter() 将匹配元素集合缩减为匹配选择器或匹配函数返回值的新元素
  10. .find() 获取当前匹配元素集合中的每个元素的后代,由选择器进行筛选
  11. .first() 将匹配元素集合缩减为集合中的 第一个元素
  12. .has() 将匹配元素集合缩减为包含特定元素的后代的集合
  13. .is() 是否存在一个匹配元素
  14. .last() 将匹配元素集合缩减为集合中的最后一个元素
  15. .map() 把当前匹配集合中的每个元素传递给函数
  16. .next() 获取下一个元素
  17. .nextAll() 获得匹配元素集合中每个元素之后的所有同辈元素
  18. .nextUntil() 获得每个元素之后所有的同辈元素直到遇到匹配选择器的元素为止
  19. .not() 从匹配元素集合中删除元素
  20. .offsetParent() 获得用于定位的第一个父元素
  21. .parent() 获得当前匹配元素集合中每个元素的父元素
  22. .parents() 获得当前匹配元素集合中每个元素的祖先元素
  23. .parentsUntil() 获得当前匹配元素集合中每个元素的祖先元素,直到遇到匹配选择器的元素为止
  24. .prev() 获得匹配元素集合中每个元素的前一个同辈元素
  25. .prevAll() 获得匹配元素集合中每个元素之前的所有同辈元素
  26. .prevUntil() 获得每个元素之前所有的同辈元素,直到遇到匹配选择器的元素为止
  27. .siblings() 获得匹配元素集合中所有元素的同辈元素
  28. .slice() 将匹配元素集合缩减为指定范围的子集
  29. addClass() 向匹配的元素添加指定的类名
  30. after() 在匹配的元素之后插入内容
  31. append() 向匹配元素集合中的每个元素结尾插入由参数指定的内容
  32. appendTo() 向目标结尾插入匹配元素集合中的每个元素
  33. attr() 设置或返回匹配元素的属性和值
  34. before() 在每个匹配的元素之前插入内容
  35. clone() 创建匹配元素集合的副本
  36. detach() dom中移除匹配元素的集合
  37. empty() 删除匹配的元素集合中所有子节点
  38. hasClass() 检查匹配的元素是否拥有指定的类
  39. html() 数组或返回匹配的元素集合中的html内容
  40. insertAfter() 把匹配的元素插入到另一个指定的元素集合的后面
  41. insertBefore() 把匹配的元素插入到另一个指定的元素集合的签名
  42. prepend() 向匹配元素集合中的每个元素开头插入由参数指定的内容
  43. prependTo() 向目标开头插入匹配元素集合中的每个元素
  44. remove() 移除所有匹配的元素
  45. removeAttr() 从所有匹配的元素中移除指定的属性
  46. removeClass() 从所有匹配的元素中删除全部或者指定的类
  47. replaceAll() 用匹配的元素替换所有匹配到的元素
  48. replaceWith() 用新内容替换匹配的元素
  49. text() 数组或返回匹配元素的内容
  50. toggleClass() 从匹配的元素中添加或删除一个类
  51. unwrap() 移除并替换指定元素的父元素
  52. val() 设置或返回匹配元素的值
  53. wrap() 把匹配额元素用指定的内容或元素包裹起来
  54. wrapAll() 把所有匹配的元素用指定的内容或元素包裹起来
  55. wrapinner() 将每一个匹配的元素的子内容用指定的内容或元素包裹起来

jQuery hide() 和 show()隐藏和显示 HTML 元素

  1. $("#hide").click(function(){
  2. $("p").hide();
  3. });
  4. $("#show").click(function(){
  5. $("p").show();
  6. });
  1. $("button").click(function(){
  2. $("p").hide(1000);
  3. });
  4. $(document).ready(function(){
  5. $(".hidebtn").click(function(){
  6. $("div").hide(1000,"linear",function(){
  7. alert("Hide() 方法已完成!");
  8. });
  9. });
  10. });

toggle() 方法来切换 hide() 和 show() 方法

  1. $("button").click(function(){
  2. $("p").toggle();
  3. });

image.png

  1. $("button").click(function(){
  2. $("#div1").fadeIn();
  3. $("#div2").fadeIn("slow");
  4. $("#div3").fadeIn(3000);
  5. });
  6. $("button").click(function(){
  7. $("#div1").fadeOut();
  8. $("#div2").fadeOut("slow");
  9. $("#div3").fadeOut(3000);
  10. });
  11. $("button").click(function(){
  12. $("#div1").fadeToggle();
  13. $("#div2").fadeToggle("slow");
  14. $("#div3").fadeToggle(3000);
  15. });
  16. $("button").click(function(){
  17. $("#div1").fadeTo("slow",0.15);
  18. $("#div2").fadeTo("slow",0.4);
  19. $("#div3").fadeTo("slow",0.7);
  20. });

image.png

  1. $("#flip").click(function(){
  2. $("#panel").slideDown();
  3. });
  4. $("#flip").click(function(){
  5. $("#panel").slideUp();
  6. });
  7. $("#flip").click(function(){
  8. $("#panel").slideToggle();
  9. });

jQuery 动画

  1. $(selector).animate({params},speed,callback);
  2. $("button").click(function(){
  3. $("div").animate({left:'250px'});
  4. });
  5. $("button").click(function(){
  6. $("div").animate({
  7. left:'250px',
  8. opacity:'0.5',
  9. height:'150px',
  10. width:'150px'
  11. });
  12. });
  13. $("button").click(function(){
  14. $("div").animate({
  15. left:'250px',
  16. height:'+=150px',
  17. width:'+=150px'
  18. });
  19. });
  20. $("button").click(function(){
  21. $("div").animate({
  22. height:'toggle'
  23. });
  24. });
  25. $("button").click(function(){
  26. var div=$("div");
  27. div.animate({left:'100px'},"slow");
  28. div.animate({fontSize:'3em'},"slow");
  29. });
  30. $("button").click(function(){
  31. var div=$("div");
  32. div.animate({height:'300px',opacity:'0.4'},"slow");
  33. div.animate({width:'300px',opacity:'0.8'},"slow");
  34. div.animate({height:'100px',opacity:'0.4'},"slow");
  35. div.animate({width:'100px',opacity:'0.8'},"slow");
  36. });

jQuery stop() 方法 停止动画

  1. $(selector).stop(stopAll,goToEnd);
  2. $("#stop").click(function(){
  3. $("#panel").stop();
  4. });
  5. $("#panel").stop(true);
  6. 停止所有动画效果而不是只停止当前动画
  1. 使用 callback 实例
  2. $("button").click(function(){
  3. $("p").hide("slow",function(){
  4. alert("段落现在被隐藏了");
  5. });
  6. });
  7. 没有 callback
  8. $("button").click(function(){
  9. $("p").hide(1000);
  10. alert("段落现在被隐藏了");
  11. });

jQuery 方法链接

  1. $("#p1").css("color","red").slideUp(2000).slideDown(2000);

wrap() wrapAll() 与 wrapInner()的区别

wrap() 方法把每个被选元素放置在指定的 HTML 内容或元素中
wrapAll() 在指定的 HTML 内容或元素中放置所有被选的元素
wrapInner() 方法使用指定的 HTML 内容或元素,来包裹每个被选元素中的所有内容 (inner HTML)

image.png

image.png

image.png

image.png

text(),html(),val()

image.png

获取属性:attr()

  1. $("button").click(function(){
  2. alert($("#runoob").attr("href"));
  3. });
  4. $("button").click(function(){
  5. $("#runoob").attr("href","http://www..com/");
  6. });
  7. $("button").click(function(){
  8. $("#runoob").attr({
  9. "href" : "http://www..com/",
  10. "title" : "111"
  11. });
  12. });

append() 在被选元素的结尾插入内容
prepend() 在被选元素的开头插入内容

after() 在被选元素之后插入内容
before() 在被选元素之前插入内容

remove() 删除被选元素(及其子元素)
empty() 从被选元素中删除子元素

addClass() 向被选元素添加一个或多个类
removeClass() 从被选元素删除一个或多个类
toggleClass() 对被选元素进行添加/删除类的切换操作
css() 设置或返回样式属性

  1. $("p").css("background-color");
  2. $("p").css("background-color","yellow");

image.png

遍历祖先

  1. $(document).ready(function(){
  2. $("span").parent();
  3. });

image.png

  1. $(document).ready(function(){
  2. $("span").parents();
  3. });

image.png

  1. $(document).ready(function(){
  2. $("span").parentsUntil("div");
  3. });

image.png

遍历后代:
children()
find()

  1. $(document).ready(function(){
  2. $("div").children();
  3. });

image.png

  1. $(document).ready(function(){
  2. $("div").find("span");
  3. });

image.png

  1. $(document).ready(function(){
  2. $("div").find("*");
  3. });

image.png

遍历同胞:

  1. siblings()
  2. next()
  3. nextAll()
  4. nextUntil()
  5. prev()
  6. prevAll()
  7. prevUntil()

siblings() 方法返回被选元素的所有同胞元素

image.png

next() 方法返回被选元素的下一个同胞元素

image.png

nextAll() 方法返回被选元素的所有跟随的同胞元素

image.png

nextUntil() 方法返回介于两个给定参数之间的所有跟随的同胞元素

image.png

first() 方法返回被选元素的首个元素
last() 方法返回被选元素的最后一个元素
eq() 方法返回被选元素中带有指定索引号的元素
索引号从 0 开始

filter() 方法 not() 方法

  1. $(document).ready(function(){
  2. $("p").filter(".url");
  3. });
  4. $(document).ready(function(){
  5. $("p").not(".url");
  6. });

ajax简介

AJAX 是与服务器交换数据的技术,属性了不重载情况下,实现对部分网页的更新。

jQuery load() 方法是简单但强大的 AJAX 方法

  1. $(selector).load(URL,data,callback);
  2. <script>
  3. $(document).ready(function(){
  4. $("button").click(function(){
  5. $("#div1").load("/ajax/demo_test.txt");
  6. });
  7. });
  8. </script>
  9. callback 参数
  10. responseTxt - 包含调用成功时的结果内容
  11. statusTXT - 包含调用的状态
  12. xhr - 包含 XMLHttpRequest 对象
  13. $("button").click(function(){
  14. $("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
  15. if(statusTxt=="success")
  16. alert("外部内容加载成功!");
  17. if(statusTxt=="error")
  18. alert("Error: "+xhr.status+": "+xhr.statusText);
  19. });
  20. });
  1. $.get() 方法 $.post() 方法
  2. $.get(URL,callback);
  3. $.post(URL,data,callback);
  4. $("button").click(function(){
  5. $.get("demo_test.php",function(data,status){
  6. alert("数据: " + data + "\n状态: " + status);
  7. });
  8. });
  9. $("button").click(function(){
  10. $.post("/ajax/demo_test_post.php",
  11. {
  12. name:"111",
  13. url:"http://www..com"
  14. },
  15. function(data,status){
  16. alert("数据: \n" + data + "\n状态: " + status);
  17. });
  18. });

jQuery.png

实例:

  1. // html
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>课程代码</title>
  6. <script type="text/javascript" src="../js/jquery/jquery-3.0.0.min.js"></script>
  7. <script type="text/javascript" src="../js/index.js"></script>
  8. <link rel="stylesheet" type="text/css" href="../css/style.css">
  9. </head>
  10. <body>
  11. <div class="header">
  12. <div class="banner">
  13. <img src="../images/###.png">
  14. <span><a href="http://coding.###.com/">实战</a></span>
  15. <span><a href="http://www.###.com/course/program">路径</a></span>
  16. <span><a href="http://www.###.com/wenda">猿问</a></span>
  17. <span><a href="http://www.###.com/article">手记</a></span>
  18. </div>
  19. <a id="loginLink" href="#">登录</a>
  20. <a id="regeLink" href="#">注册</a>
  21. </div>
  22. <div class="swipe">
  23. <div class="nav">
  24. <div class="item">
  25. <p class="title">前端开发</p>
  26. <p>
  27. <span>Html / Css</span>
  28. <span>Javascript</span>
  29. <span>Html5</span>
  30. </p>
  31. </div>
  32. <div class="item">
  33. <p class="title">后端开发</p>
  34. <p>
  35. <span>PHP / Nodejs</span>
  36. <span>Java</span>
  37. <span>C#</span>
  38. </p>
  39. </div>
  40. <div class="item">
  41. <p class="title">移动开发</p>
  42. <p>
  43. <span>Android</span>
  44. <span>iOs</span>
  45. <span>Cocos2d-x</span>
  46. </p>
  47. </div>
  48. <div class="item">
  49. <p class="title">数据处理</p>
  50. <p>
  51. <span>Mysql</span>
  52. <span>Oracle</span>
  53. <span>MongoDB</span>
  54. </p>
  55. </div>
  56. </div>
  57. <div class="ppt first"></div>
  58. </div>
  59. <div class="lessions">
  60. <p class="title">? 热门课程</p>
  61. <ul>
  62. <li>
  63. <img src="../images/004.jpg">
  64. <p>JavaScript快速入门</p>
  65. </li>
  66. <li>
  67. <img src="../images/005.jpg">
  68. <p>玩转Photoshop</p>
  69. </li>
  70. <li>
  71. <img src="../images/006.jpg">
  72. <p>代码编写规范</p>
  73. </li>
  74. <li style="margin-right:0;">
  75. <img src="../images/004.jpg">
  76. <p>JavaScript快速入门</p>
  77. </li>
  78. <li>
  79. <img src="../images/005.jpg">
  80. <p>玩转Photoshop</p>
  81. </li>
  82. <li>
  83. <img src="../images/006.jpg">
  84. <p>代码编写规范</p>
  85. </li>
  86. <li>
  87. <img src="../images/004.jpg">
  88. <p>JavaScript快速入门</p>
  89. </li>
  90. <li style="margin-right:0;">
  91. <img src="../images/005.jpg">
  92. <p>玩转Photoshop</p>
  93. </li>
  94. </ul>
  95. </div>
  96. <div class="lessions">
  97. <p class="title">☆ 最新发布</p>
  98. <ul>
  99. <li>
  100. <img src="../images/004.jpg">
  101. <p>JavaScript快速入门</p>
  102. </li>
  103. <li>
  104. <img src="../images/005.jpg">
  105. <p>玩转Photoshop</p>
  106. </li>
  107. <li>
  108. <img src="../images/006.jpg">
  109. <p>代码编写规范</p>
  110. </li>
  111. <li style="margin-right:0;">
  112. <img src="../images/004.jpg">
  113. <p>JavaScript快速入门</p>
  114. </li>
  115. <li>
  116. <img src="../images/005.jpg">
  117. <p>玩转Photoshop</p>
  118. </li>
  119. <li>
  120. <img src="../images/006.jpg">
  121. <p>代码编写规范</p>
  122. </li>
  123. <li>
  124. <img src="../images/004.jpg">
  125. <p>JavaScript快速入门</p>
  126. </li>
  127. <li style="margin-right:0;">
  128. <img src="../images/005.jpg">
  129. <p>玩转Photoshop</p>
  130. </li>
  131. </ul>
  132. </div>
  133. <div class="footer">
  134. <div class="site">
  135. <span>关于我们</span>
  136. <span>人才招聘</span>
  137. <span>讲师招募</span>
  138. <span>联系我们</span>
  139. <span>意见反馈</span>
  140. <span>友情链接</span>
  141. </div>
  142. <div class="author">
  143. ? 2016 ###.com 京ICP备13046642号
  144. </div>
  145. </div>
  146. <!-- 弹出层遮罩 -->
  147. <div id="layer-mask" class="layer-mask"></div>
  148. <!-- 弹出层窗体 -->
  149. <div id="layer-pop" class="layer-pop">
  150. <!-- 弹出层关闭按钮 -->
  151. <div id="layer-close" class="layer-close">×</div>
  152. <!-- 弹出层内容区域 -->
  153. <div id="layer-content" class="layer-content">
  154. </div>
  155. </div>
  156. <!-- 登录窗体 -->
  157. <div id="loginHtml" style="display:none;">
  158. <!-- 登录窗体 -->
  159. <div class="login">
  160. <h4 class="title">登 录</h4>
  161. <div class="item">
  162. <label>账号</label>
  163. <input id="username" class="input" name="username" type="text"/>
  164. <p class="error-msg"></p>
  165. </div>
  166. <div class="item">
  167. <label>密码</label>
  168. <input id="password" class="input" name="password" type="password"/>
  169. </div>
  170. <div class="item">
  171. <label>&nbsp;</label>
  172. <input id="loginSubmitBtn" type="submit" class="submit" value="填写好了"/>
  173. </div>
  174. </div>
  175. </div>
  176. <!-- 注册窗体 -->
  177. <div id="regeHtml" style="display:none;">
  178. <!-- 注册窗体 -->
  179. <div class="login">
  180. <h4 class="title">注 册</h4>
  181. <div class="item">
  182. <label>账号</label>
  183. <input id="username" class="input" name="username" type="text"/>
  184. <p class="error-msg"></p>
  185. </div>
  186. <div class="item">
  187. <label>密码</label>
  188. <input id="password" class="input" name="password" type="password"/>
  189. </div>
  190. <div class="item">
  191. <label>重复密码</label>
  192. <input id="repassword" class="input" name="repassword" type="password"/>
  193. </div>
  194. <div class="item">
  195. <label>&nbsp;</label>
  196. <input id="regeSubmitBtn" type="submit" class="submit" value="填写好了"/>
  197. </div>
  198. </div>
  199. </div>
  200. </body>
  201. </html>
  1. // css
  2. @charset "utf8";
  3. *{margin:0;padding:0;}
  4. body
  5. {
  6. text-align:center;
  7. font: 14px/1.5 "微软雅黑",Verdana,Tahoma,Arial,sans-serif,"宋体";
  8. line-height:30px;
  9. }
  10. ul,li
  11. {
  12. list-style:none;
  13. }
  14. /*弹出层遮罩*/
  15. .layer-mask
  16. {
  17. display: none;
  18. z-index: 99999;
  19. position: fixed;
  20. top : 0;
  21. left: 0;
  22. width: 100%;
  23. height: 100%;
  24. background: #000;
  25. opacity: 0.5;
  26. }
  27. /*弹出层窗体*/
  28. .layer-pop
  29. {
  30. display: none;
  31. z-index : 100000;
  32. position: fixed;
  33. top : 0;
  34. left : 0;
  35. right : 0;
  36. bottom: 0;
  37. margin: auto;
  38. width: 400px;
  39. height: 300px;
  40. background: #fff;
  41. }
  42. /*弹出层关闭按钮*/
  43. .layer-close
  44. {
  45. float :right;
  46. width: 24px;
  47. height: 24px;
  48. border: 3px solid #fff;
  49. text-align: center;
  50. line-height: 24px;
  51. border-radius: 50%;
  52. background: #eee;
  53. margin-right: -12px;
  54. margin-top:-12px;
  55. }
  56. .layer-close:hover
  57. {
  58. cursor: pointer;
  59. background: #ccc;
  60. color: #fff;
  61. }
  62. /*登录*/
  63. .login
  64. {
  65. }
  66. .login h4
  67. {
  68. font-size:20px;
  69. line-height:50px;
  70. }
  71. .login label
  72. {
  73. margin-right: 5px;
  74. color: #888;
  75. display: inline-block;
  76. width: 60px;
  77. text-align: right;
  78. }
  79. .login .input
  80. {
  81. border:1px solid #ccc;
  82. border-radius:3px;
  83. padding:10px 5px;
  84. width:250px;
  85. }
  86. .login .item
  87. {
  88. margin:20px auto;
  89. }
  90. .login .submit
  91. {
  92. background: #e40;
  93. border:1px solid #e40;
  94. border-radius:5px;
  95. padding:10px 5px;
  96. width:250px;
  97. color:#fff;
  98. }
  99. .login .error-msg
  100. {
  101. color:#e40;
  102. }
  103. /*慕课网效果*/
  104. /*顶部*/
  105. .header
  106. {
  107. height:80px;
  108. line-height:80px;
  109. text-align : right;
  110. margin: 0 20px;
  111. overflow:hidden;
  112. }
  113. .header .banner
  114. {
  115. float:left;
  116. }
  117. .header .banner span
  118. {
  119. font-size:18px;
  120. margin:0 15px;
  121. line-height:80px;
  122. }
  123. .header .banner img
  124. {
  125. width:150px;
  126. vertical-align: middle;
  127. margin-right:40px;
  128. }
  129. .header a
  130. {
  131. color:#666;
  132. text-decoration:none;
  133. margin-left:20px;
  134. }
  135. /*轮播*/
  136. .swipe
  137. {
  138. width: 1200px;
  139. height: 450px;
  140. margin : 0 auto;
  141. overflow:hidden;
  142. }
  143. .swipe .ppt
  144. {
  145. width:100%;
  146. height:100%;
  147. }
  148. .swipe .ppt.first
  149. {
  150. background:url(../images/001.jpg);
  151. background-size: cover;
  152. }
  153. .swipe .ppt.second
  154. {
  155. background:url(../images/002.jpg);
  156. background-size: cover;
  157. }
  158. /*导航*/
  159. .nav
  160. {
  161. position:absolute;
  162. width : 280px;
  163. height:450px;
  164. background: #000;
  165. color:#fff;
  166. filter:alpha(opacity=50); /*支持 IE 浏览器*/
  167. -moz-opacity:0.50; /*支持 FireFox 浏览器*/
  168. opacity:0.50; /*支持 Chrome, Opera, Safari 等浏览器*/
  169. }
  170. .nav .item
  171. {
  172. margin: 5px 20px;
  173. padding: 10px;
  174. text-align:left;
  175. border-bottom:1px solid #aaa;
  176. }
  177. .nav .item:hover
  178. {
  179. background:#666;
  180. cursor: pointer;
  181. }
  182. .nav .item .title
  183. {
  184. font-size:16px;
  185. margin-bottom:10px;
  186. }
  187. .nav .item span
  188. {
  189. margin-right : 9px;
  190. }
  191. /*课程排列*/
  192. .lessions
  193. {
  194. width : 1200px;
  195. margin : 0 auto;
  196. }
  197. .lessions .title
  198. {
  199. text-align:left;
  200. line-height : 45px;
  201. color:#666;
  202. font-size:16px;
  203. margin-top:30px;
  204. }
  205. .lessions ul
  206. {
  207. overflow:hidden;
  208. }
  209. .lessions li
  210. {
  211. float : left;
  212. width : 23%;
  213. margin-right: 32px;
  214. margin-top: 32px;
  215. }
  216. .lessions li img
  217. {
  218. max-width:100%;
  219. }
  220. /*页脚*/
  221. .footer
  222. {
  223. margin-top:50px;
  224. padding:50px 0;
  225. background: #eee;
  226. border-top : 1px solid #ddd;
  227. }
  228. .footer .site
  229. {
  230. line-height:100px;
  231. }
  232. .footer .site span
  233. {
  234. margin:0 20px;
  235. color :#888;
  236. }
  1. // js
  2. $(document).ready(function($){
  3. // 登录链接事件
  4. // $("#loginLink").click(function(){
  5. // // 显示弹出层遮罩
  6. // $("#layer-mask").show();
  7. // // 显示弹出层窗体
  8. // $("#layer-pop").show();
  9. // // 弹出层关闭按钮绑定事件
  10. // $("#layer-close").click(function(){
  11. // // 弹出层关闭
  12. // $("#layer-mask").hide();
  13. // $("#layer-pop").hide();
  14. // });
  15. // });
  16. // 登录链接事件
  17. $("#loginLink").click(function(){
  18. // 获取登录窗体代码
  19. var loginHtml = $("#loginHtml").html();
  20. showLayer(loginHtml,500,300,closeCallback);
  21. // 登录表单校验
  22. $("#loginSubmitBtn").click(function(){
  23. var username = $("input[name='username']").val();
  24. var password = $("input[name='password']").val();
  25. if(username === '123' && password === '123'){
  26. alert("登录成功");
  27. }else{
  28. $(".error-msg").html("账号或密码输入错误");
  29. }
  30. });
  31. });
  32. // 注册链接事件
  33. $("#regeLink").click(function(){
  34. // 获取注册窗体代码
  35. var regeHtml = $("#regeHtml").html();
  36. showLayer(regeHtml,500,350,closeCallback);
  37. // 注册表单校验
  38. $("#regeSubmitBtn").click(function(){
  39. var username = $("input[name='username']").val();
  40. var password = $("input[name='password']").val();
  41. var repassword = $("input[name='repassword']").val();
  42. if(username === 'imooc' && password === 'imooc' && repassword === password){
  43. alert("注册成功");
  44. }else{
  45. $(".error-msg").html("账号或密码输入错误");
  46. }
  47. });
  48. });
  49. // 弹出层关闭回调函数
  50. function closeCallback(){
  51. $(".error-msg").html("");
  52. }
  53. // 显示弹出层
  54. function showLayer(html,width,height,closeCallback){
  55. // 显示弹出层遮罩
  56. $("#layer-mask").show();
  57. // 显示弹出层窗体
  58. $("#layer-pop").show();
  59. // 设置弹出层窗体样式
  60. $("#layer-pop").css({
  61. width : width,
  62. height : height
  63. });
  64. // 填充弹出层窗体内容
  65. $("#layer-content").html(html);
  66. // 弹出层关闭按钮绑定事件
  67. $("#layer-close").click(function(){
  68. // 弹出层关闭
  69. hideLayer();
  70. // 关闭的回调函数
  71. closeCallback();
  72. });
  73. }
  74. // 隐藏弹出层
  75. function hideLayer(){
  76. // 弹出层关闭
  77. $("#layer-mask").hide();
  78. $("#layer-pop").hide();
  79. }
  80. });

image.png

image.png

结言

好了,欢迎在留言区留言,与大家分享你的经验和心得。

感谢你学习今天的内容,如果你觉得这篇文章对你有帮助的话,也欢迎把它分享给更多的朋友,感谢。

原文链接:http://www.cnblogs.com/dashucoding/p/10750368.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号