前端框架之jQuery

一 iQuery是什么

jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team

jQuery是继prototype之后又一个优秀的Javascript框架。其宗旨是——WRITE LESS,DO MORE!

它是轻量级的js库(压缩后只有21k) ,这是其它的js库所不及的,它兼容CSS3,还兼容各种浏览器

jQuery是一个快速的,简洁的javaScript库,使用户能更方便地处理HTMLdocuments、events、实现动画效果,并且方便地为网站提供AJAX交互。

jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。


二 什么是jQuery对象?

jQuery 对象就是通过jQuery包装DOM对象后产生的对象。jQuery 对象是 jQuery 独有的. 如果一个对象是 jQuery 对象, 那么它就可以使用 jQuery 里的方法: $(“#test”).html();

$("#test").html()    
      //意思是指:获取ID为test的元素内的html代码。其中html()是jQuery里的方法 
      // 这段代码等同于用DOM实现代码: document.getElementById(" test ").innerHTML; 
      //虽然jQuery对象是包装DOM对象后产生的,但是jQuery无法使用DOM对象的任何方法,同理DOM对象也不能使用jQuery里的方法.乱使用会报错
      //约定:如果获取的是 jQuery 对象, 那么要在变量前面加上$. 
var $variable = jQuery 对象
var variable = DOM 对象
$variable[0]:jquery对象转为dom对象      $("#msg").html(); $("#msg")[0].innerHTML

jquery的基础语法:$(selector).action()

使用时到官网下载保存(打开后按Ctrl+s可以保存),放到项目路径下引入


三 寻找元素(选择器和筛选器) 

3.1   选择器

3.1.1 基本选择器  

$("*")  $("#id")   $(".class")  $("element")  $(".class,p,div")


3.1.2 层级选择器 

$(".outer div")  $(".outer>div")   $(".outer+div")  $(".outer~div")


3.1.3 基本筛选器      

$("li:first")  $("li:eq(2)")  $("li:even") $("li:gt(1)")


3.1.4 属性选择器   

$('[id="div1"]')

 

3.1.5 表单选择器     

$("[type='text']")----->$(":text")         注意只适用于input标签  : $("input:checked")


实例之左侧菜单

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>left_menu</title>

   <style>
         .menu{
             height500px;
             width30%;
             background-color: gainsboro;
             float: left;
         }
         .content{
             height500px;
             width70%;
             background-color: rebeccapurple;
             float: left;
         }
        .title{
            line-height50px;
            background-color#425a66;
            color: forestgreen;}
        .hide{
            display: none;
        }
   </style>
</head>
<body>

<div class="outer">
   <div class="menu">
       <div class="item">
           <div class="title">菜单一</div>
           <div class="con">
               <div>111</div>
               <div>111</div>
               <div>111</div>
           </div>
       </div>
       <div class="item">
           <div class="title">菜单二</div>
           <div class="con hide">
               <div>111</div>
               <div>111</div>
               <div>111</div>
           </div>
       </div>
       <div class="item">
           <div class="title">菜单三</div>
           <div class="con hide">
               <div>111</div>
               <div>111</div>
               <div>111</div>
           </div>
       </div>

   </div>
   <div class="content"></div>

</div>
<script src="jquery-3.2.1.js"></script>
<script>
          $(".item .title").click(function ({
               $(this).next().removeClass("hide").parent().siblings().children(".con").addClass("hide");

//                $(this).next().removeClass("hide");
//                $(this).parent().siblings().children(".con").addClass("hide");
          })
</script>
</body>
</html>

实例之tab切换

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>tab</title>
   <script src="jquery-3.3.1.min.js"></script>
   <script>
          function tab(self){
              var index=$(self).attr("xxx");
              $("#"+index).removeClass("hide").siblings().addClass("hide");
              $(self).addClass("current").siblings().removeClass("current");

          }
   </script>
   <style>
       *{
           margin0px;
           padding0px;
       }
       .tab_outer{
           margin0px auto;
           width60%;
       }
       .menu{
           background-color#cccccc;
           /*border: 1px solid red;*/
           line-height40px;
       }
       .menu li{
           display: inline-block;
       }
       .menu a{
           border-right1px solid red;
           padding11px;
       }
       .content{
           background-color: tan;
           border1px solid green;
           height300px;
       }
       .hide{
           display: none;
       }

       .current{
           background-color: darkgray;
           color: yellow;
           border-top: solid 2px rebeccapurple;
       }
   </style>
</head>
<body>
     <div class="tab_outer">
         <ul class="menu">
             <li xxx="c1" class="current" onclick="tab(this);">菜单一</li>
             <li xxx="c2" onclick="tab(this);">菜单二</li>
             <li xxx="c3" onclick="tab(this);">菜单三</li>
         </ul>
         <div class="content">
             <div id="c1">内容一</div>
             <div id="c2" class="hide">内容二</div>
             <div id="c3" class="hide">内容三</div>
         </div>

     </div>
</body>
</html>

3.2 筛选器

3.2.1  过滤筛选器     

$("li").eq(2)  $("li").first()  $("ul li").hasclass("test")

  

3.2.2  查找筛选器

$("div").children(".test")     $("div").find(".test")                              
$(".test").next()    $(".test").nextAll()    $(".test").nextUntil()                          
$("div").prev()  $("div").prevAll()  $("div").prevUntil()                      
$(".test").parent()  $(".test").parents()  $(".test").parentUntil() 
$("div").siblings()


四 操作元素(属性,css,文档处理)

4.1 属性操作
--------------------------属性
$("").attr();
$("").removeAttr();
$("").prop();
$("").removeProp();
--------------------------CSS类
$("").addClass(class|fn)
$("").removeClass([class|fn])
--------------------------HTML代码/文本/值
$("").html([val|fn])
$("").text([val|fn])
$("").val([val|fn|arr])
---------------------------
$("").css("color","red")


jQuery循环的两种方式

jquery循环的两种方式
方式一
li=[10,20,30,40]
dic={name:"yuan",sex:"male"}
$.each(li,function(i,x){
    console.log(i,x)
})

方式二
$("tr").each(function(){
   console.log($(this).html())
})


实例之全反选

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="jquery-3.3.1.min.js"></script>
  <script>
           function selectall(){
               $("table :checkbox").prop("checked",true)
           }
           function cancel(){
               $("table :checkbox").prop("checked",false)
           }
           function reverse(){
               $("table :checkbox").each(function(){
                   $(this).prop("checked",!$(this).prop("checked"));
               });
           }
  </script>
</head>
<body>

   <button onclick="selectall();">全选</button>
   <button onclick="cancel();">取消</button>
   <button onclick="reverse();">反选</button>

   <table border="1">
       <tr>
           <td><input type="checkbox"></td>
           <td>111</td>
       </tr>
       <tr>
           <td><input type="checkbox"></td>
           <td>222</td>
       </tr>
       <tr>
           <td><input type="checkbox"></td>
           <td>333</td>
       </tr>
       <tr>
           <td><input type="checkbox"></td>
           <td>444</td>
       </tr>
   </table>
</body>
</html>


实例之模态对话框

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <style>
       .back{
           background-color: rebeccapurple;
           height2000px;
       }

       .shade{
           position: fixed;
           top0;
           bottom0;
           left:0;
           right0;
           background-color: coral;
           opacity0.4;
       }

       .hide{
           display: none;
       }

       .models{
           position: fixed;
           top50%;
           left50%;
           margin-left: -100px;
           margin-top: -100px;
           height200px;
           width200px;
           background-color: gold;

       }
   </style>
</head>
<body>
<div class="back">
   <input id="ID1" type="button" value="click" onclick="action1(this)">
</div>

<div class="shade hide"></div>
<div class="models hide">
   <input id="ID2" type="button" value="cancel" onclick="action2(this)">
</div>


<script src="jquery-3.3.1.min.js"></script>
<script>

   function action1(self){
       $(self).parent().siblings().removeClass("hide");

   }
   function action2(self){
       //$(self).parent().parent().children(".models,.shade").addClass("hide")
   $(self).parent().addClass("hide").prev().addClass("hide")
   }
</script>
</body>
</html>


4.2 文档处理

//创建一个标签对象
   $("<p>")
//内部插入
   $("").append(content|fn)      ----->$("p").append("<b>Hello</b>");
   $("").appendTo(content)       ----->$("p").appendTo("div");
   $("").prepend(content|fn)     ----->$("p").prepend("<b>Hello</b>");
   $("").prependTo(content)      ----->$("p").prependTo("#foo");
//外部插入
   $("").after(content|fn)       ----->$("p").after("<b>Hello</b>");
   $("").before(content|fn)      ----->$("p").before("<b>Hello</b>");
   $("").insertAfter(content)    ----->$("p").insertAfter("#foo");
   $("").insertBefore(content)   ----->$("p").insertBefore("#foo");
//替换
   $("").replaceWith(content|fn) ----->$("p").replaceWith("<b>Paragraph. </b>");
//删除
   $("").empty()
   $("").remove([expr])
//复制
   $("").clone([Even[,deepEven]])


4.3 css操作

CSS
       $("").css(name|pro|[,val|fn])
   位置
       $("").offset([coordinates])
       $("").position()
       $("").scrollTop([val])
       $("").scrollLeft([val])
   尺寸
       $("").height([val|fn])
       $("").width([val|fn])
       $("").innerHeight()
       $("").innerWidth()
       $("").outerHeight([soptions])
       $("").outerWidth([options])


实例返回顶部

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <script src="js/jquery-2.2.3.js"></script>
   <script>


         window.onscroll=function(){

             var current=$(window).scrollTop();
             console.log(current)
             if (current>100){

                 $(".returnTop").removeClass("hide")
             }
             else {
             $(".returnTop").addClass("hide")
         }
         }


          function returnTop(){
//               $(".div1").scrollTop(0);

              $(window).scrollTop(0)
          }




   </script>
   <style>
       body{
           margin0px;
       }
       .returnTop{
           height60px;
           width100px;
           background-color: darkgray;
           position: fixed;
           right0;
           bottom0;
           color: greenyellow;
           line-height60px;
           text-align: center;
       }
       .div1{
           background-color: orchid;
           font-size5px;
           overflow: auto;
           width500px;
       }
       .div2{
           background-color: darkcyan;
       }
       .div3{
           background-color: aqua;
       }
       .div{
           height300px;
       }
       .hide{
           display: none;
       }
   </style>
</head>
<body>
    <div class="div1 div">
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>
        <p>hello</p>

    </div>
    <div class="div2 div"></div>
    <div class="div3 div"></div>
    <div class="returnTop hide" onclick="returnTop();">返回顶部</div>
</body>
</html>




识别图中二维码,领取python全套视频资料

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
所有效果说明:基本的鼠标互动:拖拽(drag and dropping)、排序(sorting)、选择(selecting)、缩放(resizing) 各种互动效果:手风琴式的折叠菜单(accordions)、日历(date pickers)、对话框(dialogs)、滑动条 (sliders)、表格排序(table sorters)、页签(tabs) 放大镜效果(magnifier)、阴影效果(shadow) 第一部分:鼠标交互 1.1 Draggables:拖拽所需文件: ui.mouse.js ui.draggable.js ui.draggable.ext.js 用法:文件载入后,可以拖拽class = "block"的层 $(document).ready(function(){ $(".block").draggable(); }); draggable(options)可以跟很多选项选项说明:http://docs.jquery.com/UI/Draggables/draggable#options 选项实例:http://dev.jquery.com/view/trunk/plugins/ui/tests/draggable.html 1.2 Droppables 所需要文件,drag drop ui.mouse.js ui.draggable.js ui.draggable.ext.js ui.droppable.js ui.droppable.ext.js 用法: $(document).ready(function(){ $(".block").draggable({helper: 'clone'}); $(".drop").droppable({ accept: ".block", activeClass: 'droppable-active', hoverClass: 'droppable-hover', drop: function(ev, ui) { $(this).append("<br>Dropped!"); } }); }); 选项说明:http://docs.jquery.com/UI/Droppables/droppable#options 选项实例:http://dev.jquery.com/view/trunk/plugins/ui/tests/droppable.html 1.3 Sortables 排序所需要的文件 jquery.dimensions.js ui.mouse.js ui.draggable.js ui.droppable.js ui.sortable.js 用法: $(document).ready(function(){ $("#myList").sortable({}); }); dimensions文档http://jquery.com/plugins/project/dimensions 选项说明:http://docs.jquery.com/UI/Sortables/sortable#options 选项实例:http://dev.jquery.com/view/trunk/plugins/ui/demos/ui.sortable.html 1.4 Selectables 选择所需要的文件 jquery.dimensions.js ui.mouse.js ui.draggable.js ui.droppable.js ui.selectable.js 用法: $(document).ready(function(){ $("#myList").selectable(); }); 选项说明:http://docs.jquery.com/UI/Selectables/selectable#options 选项实例:http://dev.jquery.com/view/trunk/plugins/ui/tests/selectable.html 1.5 Resizables改变大小所需要的文件 ,此例子需要几个css文件 jquery.dimensions.js ui.mouse.js ui.resizable.js 用法: $(document).ready(function(){ $("#example").resizable(); }); CSS文件:http://dev.jquery.com/view/trunk/themes/flora/flora.all.css 选项说明:http://docs.jquery.com/UI/Resizables/resizable#options 选项实例:http://dev.jquery.com/view/trunk/plugins/ui/demos/ui.resizable.html 第二部分:互动效果 2.1 Accordion 折叠菜单所需要的文件: ui.accordion.js jquery.dimensions.js 用法: $(document).ready(function(){ $("#example").accordion(); }); CSS文件:http://dev.jquery.com/view/trunk/themes/flora/flora.all.css 选项说明:http://docs.jquery.com/UI/Accordion/accordion#options 选项实例:http://dev.jquery.com/view/trunk/plugins/accordion/?p=1.1.1 2.2 dialogs 对话框所需要的文件: jquery.dimensions.js ui.dialog.js ui.resizable.js ui.mouse.js ui.draggable.js 用法: $(document).ready(function(){ $("#example").dialog(); }); CSS文件:http://dev.jquery.com/view/trunk/themes/flora/flora.all.css 选项说明:http://docs.jquery.com/UI/Dialog/dialog#options 选项实例:http://dev.jquery.com/view/trunk/plugins/ui/tests/dialog.html 2.3 sliders 滑动条所需要的文件 jquery.dimensions.js ui.mouse.js ui.slider.js 用法: $(document).ready(function(){ $("#example").slider(); }); CSS文件:http://dev.jquery.com/view/trunk/themes/flora/flora.all.css 选项说明:http://docs.jquery.com/UI/Slider/slider#options 选项实例:http://dev.jquery.com/view/trunk/plugins/ui/demos/ui.slider.html 2.4 Tablesorter表格排序所需要的文件 ui.tablesorter.js 用法: $(document).ready(function(){ $("#example").tablesorter({sortList:[[0,0],[2,1]], widgets: ['zebra']}); }); CSS文件:http://dev.jquery.com/view/trunk/themes/flora/flora.all.css 选项说明:http://docs.jquery.com/Plugins/Tablesorter/tablesorter#options 选项实例:http://tablesorter.com/docs/#Demo 2.5 tabs页签(对IE支持不是很好) 所需要的文件 ui.tabs.js 用法: $(document).ready(function(){ $("#example > ul").tabs(); }); CSS文件:http://dev.jquery.com/view/trunk/themes/flora/flora.all.css 选项说明:http://docs.jquery.com/UI/Tabs/tabs#initialoptions 选项实例:http://dev.jquery.com/view/trunk/plugins/ui/tests/tabs.html tabs ext http://stilbuero.de/jquery/tabs_3/rotate.html 第三部分:效果 3.1 Shadow 阴影实例http://dev.jquery.com/view/trunk/plugins/ui/demos/ui.shadow.html 3.2 Magnifier 放大实例http://dev.jquery.com/view/trunk/plugins/ui/demos/ui.magnifier.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值