前端框架之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全套视频资料

猜你喜欢

转载自www.cnblogs.com/IT-Scavenger/p/9249407.html