日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

前端之jQuery

 路人甲Java 2021-09-16

jQuery對(duì)象

jQuery對(duì)象是通過(guò)jQuery包裝DOM對(duì)象后產(chǎn)生的對(duì)象

注意:jQuery對(duì)象只能使用jQuery里的方法,DOM對(duì)象只能使用DOM對(duì)象的方法

jQuery基礎(chǔ)知識(shí)點(diǎn)

查找標(biāo)簽

基本選擇器

  • id選擇器: $('#id')
  • 標(biāo)簽選擇器:$('tagName')
  • class選擇器:$(".className“)
  • 配合使用:$("div.c1")  //找到有c1的div標(biāo)簽
  • 所有元素選擇器:$("*")
  • 組合選擇器:$("#id, .className, tagName")

層級(jí)選擇器:

  • $("div span");   //div標(biāo)簽下所有后代span標(biāo)簽(子標(biāo)簽 ,子子標(biāo)簽...)
  • $("div > sapn"); //div標(biāo)簽下所有兒子span標(biāo)簽
  • $("div + span"); //緊挨著div標(biāo)簽的第一span元素
  • $("div~span");   //div標(biāo)簽下所有兄弟元素span.

基本篩選器:

:first // 第一個(gè)
:last // 最后一個(gè)
:eq(index)// 索引等于index的那個(gè)元素
:even // 匹配所有索引值為偶數(shù)的元素,從 0 開(kāi)始計(jì)數(shù)
:odd // 匹配所有索引值為奇數(shù)的元素,從 0 開(kāi)始計(jì)數(shù)
:gt(index)// 匹配所有大于給定索引值的元素
:lt(index)// 匹配所有小于給定索引值的元素
:not(元素選擇器)// 移除所有滿(mǎn)足not條件的標(biāo)簽
:has(元素選擇器)// 選取所有包含一個(gè)或多個(gè)標(biāo)簽在其內(nèi)的標(biāo)簽(指的是從后代元素找)
//例子
$('ul li:first')
S.fn.init [li, prevObject: S.fn.init(1)]
$('ul li:gt(3)')
S.fn.init(6) [li, li#l1, li, li.cl1, li, li, prevObject: S.fn.init(1)]
$('ul li:has(#l1)')
S.fn.init [prevObject: S.fn.init(1)]length: 0prevObject: S.fn.init [document]__proto__: Object(0)
$('ul li:not(.cl1)')
S.fn.init(9) [li, li, li, li, li, li#l1, li, li, li, prevObject: S.fn.init(1)]
$('ul li:odd')
S.fn.init(5) [li, li, li#l1, li.cl1, li, prevObject: S.fn.init(1)]

屬性選擇器:

  • [屬性】
  • [屬性名=value]     
  • [屬性!=value]
//例子
$('[username]')
S.fn.init(2) [input, input, prevObject: S.fn.init(1)]
$('[username="hello"]')
S.fn.init [input, prevObject: S.fn.init(1)]

表單篩選器:

:text
:password
:file
:radio
:checkbox

:submit
:reset
:button

//例子:
$(":checkbox")  // 找到所有的checkbox

//表單對(duì)象屬性:
:enabled
:disabled
:checked
:selected
//例子
$("input:enabled")  // 找到可用的input標(biāo)簽
$(":selected")  // 找到所有被選中的option
$(":checked")  // 會(huì)將checked和selected都拿到,一般使用的時(shí)候會(huì)加個(gè) 限制條件,如:
$("input:selected")  //找到被選中的input標(biāo)簽

篩選器方法:

//下一個(gè)元素:
$("#id").next() 
$("#id").nextAll()
$("#id").nextUntil("#d1")   //尋找下一個(gè)元素,直到#d1位置為止,不包括#d1標(biāo)簽

//上一個(gè)元素
$("#id").prev()
$("#id").prevAll()
$("#id").prevUntil("#i2")    //尋找上一個(gè)元素,直到#d1位置為止,不包括#d1標(biāo)簽

//父元素
$("#id").parent()
$("#id").parents()  // 查找當(dāng)前元素的所有的父輩元素
$("#id").parentsUntil(".c1") // 查找當(dāng)前元素的所有的父輩元素,直到遇到匹配的.c1元素為止。不包括.c1元素

//兒子和兄弟元素
$("#id").children();// 兒子
$("#id").siblings();// 兄弟

 

jQuery的一些方法:

  • $("div").find("p")   //查找div標(biāo)簽下的p標(biāo)簽   等價(jià)于$("div p")
  • $("div").filter(".c1")  //查找含有c1樣式類(lèi)的div標(biāo)簽。 等價(jià)于$("div.c1)
  • $("div").first()
  • $("div").last()
  • $("div").not()
  • $("div").has()
  • $("div").eq()

 標(biāo)簽操作

樣式操作

jQuery                             js
addClass();                        classList.add()
removeClass();                     classList.remove()
hasClass();  //判斷樣式是否存在       classList.contains()
toggleClass();//有則刪去,無(wú)則添加    classList.toggle()
//例子
$("#d1").toggleClass('c1')
S.fn.init [div#d1.c2]
$("#d1").addClass('c1')
S.fn.init [div#d1.c2.c1]
$("#d1").toggleClass('c2')
S.fn.init [div#d1.c1]

位置操作

  • offset()   獲取匹配元素在當(dāng)前窗口的相對(duì)偏移或這種元素位置
  • position()  獲取匹配元素相對(duì)父元素的偏移
  • scrollTop()  獲取匹配元素相對(duì)滾動(dòng)條頂部的偏移
  • scrollLeft()  獲取匹配元素相對(duì)滾動(dòng)條左側(cè)的偏移

尺寸:

  • height()         高度
  • widght()        寬度
  • innerHeight()     
  • innerWidth()  
  • outerHeght()      
  • outerWidth()

 

文本操作

//jQuery                           js
text()                             innerText()
html()                             innerHtml()
//例子
$('div').text()     //獲取標(biāo)簽文本
$('div').text('hello world')    //設(shè)置標(biāo)簽文本內(nèi)容
$('div').html()    //獲取標(biāo)簽html文本
$('div').html('<h1>hello world</h1>')  //設(shè)置html文本內(nèi)容

取值操作

//jQuery                    js
.val()                      .value()
//例子:
$('input').val()             //獲取值
$('input').val('hello')      //設(shè)置值

//例子

S.fn.init [input#ip]
$('#ip')[0]
<input type=?"file" id=?"ip">?
$('#ip')[0].files[0]   //$('#ip')[0]先轉(zhuǎn)化為原js對(duì)象,再用原js對(duì)象中files獲取文件信息
File {name: "1.png", lastModified: 1600868737120, lastModifiedDate: Wed Sep 23 2020 21:45:37 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間), webkitRelativePath: "", size: 21844, …}
$('#ip')[0].files
FileList {0: File, length: 1}0: File {name: "1.png", lastModified: 1600868737120, lastModifiedDate: Wed Sep 23 2020 21:45:37 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間), webkitRelativePath: "", size: 21844, …}length: 1__proto__: FileList

 屬性操作

 
//jQuery                          
attr(屬性)         //獲取屬性值               
attr(屬性,屬性值)   //設(shè)置屬性值
removeAttr(屬性)  //刪除屬性
//對(duì)應(yīng)的js操作方法
setAttribute()
getAttribute()
removeAttribute()
//用于checkbox和radio
prop()    //獲取屬性
removeProp()  //移除屬性
//例子
let $pEle=$('#d1')
undefined

$pEle.attr('username')
"hello"
$pEle.attr('username','hello world')

$pEle.attr('username')
"hello world"
$pEle.attr('value','11')

let $iEle=$('#d2')

$iEle.prop('checked')
false
$iEle.prop('checked')
true
//attr所指的屬性時(shí)HTML標(biāo)簽屬性,而prop是指DOM對(duì)象屬性

注意:對(duì)于標(biāo)簽上有的能夠看到的屬性和自定義屬性用attr()方法

     對(duì)于返回布爾值比如checkbox,radion,option這三個(gè)標(biāo)簽是否被選中,用prop方法

文檔操作

//js                    jQuery
createElement('p')      $('<p>')   //創(chuàng)建新標(biāo)簽
appendChild()           append()  //追加新標(biāo)簽
//例子
let $pEle=$('<p>')  //創(chuàng)建p標(biāo)簽
undefined
$pEle.text('你好 世界 ')
S.fn.init [p]
$pEle.attr('id','p2')

$('#d1').append($pEle)   //d1內(nèi)部尾部追加p標(biāo)簽
S.fn.init [div#d1]
$pEle.appendTo($('#d1'))
S.fn.init [p#p2, prevObject: S.fn.init(1)]

$('#d3').prepend($pEle)  //內(nèi)部頭部追加
S.fn.init [div#d3]
$pEle.prependTo($('#d3'))
S.fn.init [p, prevObject: S.fn.init(1)]

$('#d3').after($pEle)   //放在id=d3的標(biāo)簽后面
S.fn.init [div#d3]
$pEle.insertAfter($('#d3'))
S.fn.init [p, prevObject: S.fn.init(1)]

$('#d3').before($pEle)  //放在id=d3的標(biāo)簽前面
S.fn.init [div#d3]
$pEle.insertBefore($('#d3'))
S.fn.init [p, prevObject: S.fn.init(1)]

$('#d1').remove()  //刪除標(biāo)簽
S.fn.init [div#d1]
$('#p1').empty()   //清空標(biāo)簽內(nèi)容
S.fn.init {}

 事件

事件綁定方式:

//第一種
$('#d1').click(functino(){
   alert('1'); 
})
//第二種

語(yǔ)法:.on( events [, selector(選擇器) ],function(){})
$('#d1').on('click',function(){
    alert('1')
})
$('#d1').on('click','div',function(){
    alert('1')
})

注意:DOM定義的事件可以用 .on()方法來(lái)綁定事件,但是jQuery定義的事件就不可以

常用事件有:

  • click     點(diǎn)擊
  • hover   鼠標(biāo)懸浮在目標(biāo)上面
  • blur      輸入域失去焦點(diǎn)
  • focus    輸入域獲取焦點(diǎn)(點(diǎn)擊input輸入框)
  • change  改變,一般用在選擇、勾選框
  • keyup     釋放鍵盤(pán)上的按鍵時(shí)觸發(fā)
  • keydown  按下按鍵時(shí)觸發(fā)
  • input      用戶(hù)輸入時(shí)觸發(fā)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <style>
        .left{
            width: 20%;
            height: 800px;
            display: block;
            float: left;
            background-color: grey;
            position: fixed;
        }
        .menu{
            font-size: 32px;
            font-weight: bold;
            text-align: center;
            color: white;
        }
        .item{
            color:whitesmoke;
            font-size: 20px;
            border: 3px solid red;
        }
        .hidden{
            display: none;
        }
    </style>
</head>
<body>
    <div class="left">
        <div class="menu">菜單一
            <div class="item ">學(xué)習(xí)</div>
            <div class="item ">游戲</div>
            <div class="item ">讀書(shū)</div>
        </div>
        <div class="menu">菜單二
            <div class="item ">學(xué)習(xí)</div>
            <div class="item ">游戲</div>
            <div class="item">讀書(shū)</div>
        </div>
        <div class="menu">菜單三
            <div class="item">學(xué)習(xí)</div>
            <div class="item">游戲</div>
            <div class="item">讀書(shū)</div>
        </div>
    </div>
    <script>
        $('.menu').on('click',function () {
            $('.item').addClass('hidden') //將所有的子菜單隱藏起來(lái)
            $(this).children().removeClass('hidden')  //將選擇的菜單下子菜單取消隱藏
        })
    </script>
</body>
</html>
左側(cè)菜單
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <style>
        .cover{
            position: fixed;
            top:0px;
            left: 0px;
            right: 0px;
            bottom: 0px;
            background-color: black;
            opacity: 0.4;
            z-index: 99;
        }
        .modal{
            top:50%;
            left: 50%;
            width: 600px;
            height: 300px;
            margin-top: -150px;
            margin-left: -300px;
            background-color: white;
            position: fixed;
            z-index: 100;
        }
        .hidden{
            display: none;
        }
    </style>
</head>
<body>
    <div>我是最底層</div>
    <button id="d1">點(diǎn)擊注冊(cè)</button>
    <div class="cover hidden" id="d2"></div>
    <div class="modal hidden" id="d3">
        <p>
            usename:<input type="text">
        </p>
        <p>
            password:<input type="text">
        </p>
        <input type="button" value="commit" >
        <input type="button" value="quxiao" id="i1">
    </div>
    <script>
        //注冊(cè)
        $('#d1').click(function () {
            $('#d2').removeClass('hidden')
            $('#d3').removeClass('hidden')
        })
        //取消注冊(cè)
        $('#i1').click(function () {
            console.log(this)
            $('#d2').addClass('hidden')
            $('#d3').addClass('hidden')
        })
    </script>
</body>
</html>
模擬百度注冊(cè)界面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <style>
        span{
            color:red;
        }
        p{
            border: 3px solid blue;
        }
    </style>
</head>
<body>
    <div>
        user:<input id="i1" type="text">
        <span></span>
    </div>
    <div>
        password:<input id="i2" type="password">
        <span></span>
    </div>
    <div>
        <input id="b1" type="button" value="提交">
        <input type="button" value="取消">
    </div>
    <p>hello world</p>
    <script>
        $('#b1').click(function () {
            let user=$('#i1').val()
            let password=$('#i2').val()
            if(!user){
                $('span').first().text('用戶(hù)名不能為空')
            }
            if(!password){
                $('span').last().text('密碼不能為空')
            }
            $('input').focus(function () {
                $('span').first().text('')
            })
        })
        //input事件
        $('#i1').on('input',function () {
            console.log(this.value)
        })
        //鼠標(biāo)懸浮事件
        $('p').hover(
            function () {
                alert('我來(lái)了')
        },function () {

            })

    </script>
</body>
</html>
模擬input、hover、click事件例子
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <style>
        #d1{
            width: 100%;
            height: 500px;
            background-color: hotpink;
        }
        #d2{
            width: 100%;
            height: 500px;
            background-color: greenyellow;
        }
        #d3{
            width: 100%;
            height: 500px;
            background-color: deepskyblue;
        }
        #p1{
            width: 100px;
            height: 40px;
            background-color: grey;
            position: fixed;
            right: 20px;
            bottom: 20px;
            text-align: center;
            padding-top: 20px;
            border-radius: 20%;
            text-decoration: none; //去除a標(biāo)簽下的下劃線(xiàn)
        }
        .hidden{
            display: none;
        }
    </style>
</head>
<body>
<div id="d1"></div>
<div id="d2"></div>
<div id="d3"></div>
<a id="p1" class="hidden" href="#d1">返回頂部</a>
<script>
    $(window).scroll(function () {
        if($(window).scrollTop()>500){
            $('#p1').removeClass('hidden')
        }
        else {
            $('#p1').addClass('hidden')
        }
    })
</script>

</body>
</html>
返回頂部

移除事件:把on改成off,就是移除 .on()綁定的事件

阻止后續(xù)事件執(zhí)行:事件函數(shù)中添加   return false; (常用于阻止表單提交等)或者e.preventDefault()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <style>
        #p1{
            color: red;
        }
    </style>
</head>
<body>
    <form action="">
        <p id="p1"></p>
        <input type="submit" id="i1">
    </form>
    <script>
        $('#i1').click(function (e) {
            $('#p1').text('提交成功')
            <!-- 阻止input其他事件的發(fā)生,比如input關(guān)聯(lián)的表單事件提交-->
            return false
            <!-- 
            第二種
            e.preventDefault()
             -->
        })
    </script>
</body>
</html>    
阻止表單提交

阻止事件冒泡:添加e.stopPropagation()或return false

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <div id="d1">div
        <p id="p1">p
            <span id="s1">span</span>
        </p>
    </div>
    <script>
        $('#d1').click(function () {
            alert('div')
        })
        $('#p1').click(function () {
            alert('p')
        })
        $('#s1').click(function (e) {
            alert('span')
            // return false     //阻止事件冒泡
            e.stopPropagation()
        })
    </script>

</body>
</html>
阻止事件冒泡

頁(yè)面載入

//當(dāng)頁(yè)面加載完觸發(fā):
$(function(){
}
/*
與js的window.onload的區(qū)別
1、window.onload()函數(shù)有覆蓋現(xiàn)象,必須等待圖片資源加載完才能調(diào)用
2、$(functino(){}不會(huì)出現(xiàn)覆蓋,而且只要文檔加載完畢就可以調(diào)用,不用等待圖片資源加載
*/

事件委托

事件委托利用的就是冒泡事件的原理,通過(guò)在父元素指定一個(gè)事件來(lái)處理子元素下的一個(gè)類(lèi)型事件,比如在ul元素綁定一個(gè)點(diǎn)擊事件,來(lái)處理子元素li的點(diǎn)擊事件。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
</ul>

<script>
    //為ul下的所有l(wèi)i綁定點(diǎn)擊事件
    $('ul').on('click','li',function () {
        alert('點(diǎn)我準(zhǔn)沒(méi)錯(cuò)')
    })
</script>
</body>
</html>

jQuery的each()方法:

實(shí)質(zhì):就是遍歷一個(gè)jQuery對(duì)象,為jQuery對(duì)象里面每個(gè)元素傳遞給函數(shù)執(zhí)行

語(yǔ)法:$('#id').each(function(index,Element)        index是數(shù)字索引,Elenment是元素對(duì)象

//例子   第一種
$('li').each(function(index,ele){ console.log(index,ele)})
VM766:1 0 <li>?1?</li>?
VM766:1 1 <li>?2?</li>?
VM766:1 2 <li>?3?</li>?
S.fn.init(3) [li, li, li, prevObject: S.fn.init(1)]
//第二種
let li_list=[1,2,3,4]
$.each(li_list,function(index,ele){console.log(index,ele)})
VM1056:1 0 1
VM1056:1 1 2
VM1056:1 2 3
VM1056:1 3 4
(4) [1, 2, 3, 4]

注意:在遍歷過(guò)程中可以使用return false提前結(jié)束each循環(huán)

 jQuery.data()方法:

給標(biāo)簽儲(chǔ)存數(shù)據(jù),用戶(hù)肉眼看不到

$('li').data('name','hello')    
//給li添加name屬性,屬性值為“hello” $('li').first().data('name') "hello" //移除name屬性 $('li').removeData('name') $('li').first().data('name') undefined

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多