JQuery初步學習

文章目錄

  • 一、前言
  • 二、概述
    • 2.1 介紹
    • 2.2 安裝
  • 三、語法
    • 3.1 文檔就緒
    • 3.2 選擇器
  • 四、事件
    • 4.1 概述
    • 4.2 事件綁定/解綁
    • 4.3 一次性事件
    • 4.4 事件委托
    • 4.5 自定義事件
  • 五、效果
    • 5.1 隱藏/顯示
    • 5.2 淡入淡出
    • 5.3 滑動
    • 5.4 動畫
  • 六、鏈
  • 七、HTML
    • 7.1 內容/屬性
    • 7.2 元素操作
    • 7.3 類屬性
    • 7.4 樣式屬性
    • 7.5 遍歷dom
    • 7.6 過濾元素

一、前言

官方網站:https://jquery.com/

參考資料:https://learn.jquery.com/about-jquery/、https://www.runoob.com/jquery/jquery-tutorial.html

二、概述

2.1 介紹

jQuery 是一個快速、小型且功能豐富的 JavaScript 庫。它可以在多個瀏覽器上通過使用API來遍歷HTML 文檔、事件處理、動畫和 Ajax 等

2.2 安裝

下載鏈接:https://jquery.com/download/,選擇適宜的版本進入頁面后,右鍵保存為文件,然后將下載好的文件放在網頁的同一目錄下,就可以開始使用JQuery了

除此之外,也可以使用CDN來使用JQuery,例如使用百度的CDN:

<head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>

三、語法

3.1 文檔就緒

在DOM加載完成后才開始對DOM進行操作,基本格式為:

$(document).ready(function(){// JQuery代碼
});// 也可以這么寫
$(function(){// JQuery代碼
});

舉個栗子:當刷新網頁時,會觸發警告框

<html><head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$( document ).ready(function() {alert( "document loaded" );});</script></head><body><p>Hello world!</p></body>
</html>

也可以寫成下面形式,效果都是一樣的

<html><head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$( window ).on( "load", function() {alert( "window loaded" );});</script></head><body><p>Hello world!</p></body>
</html>

3.2 選擇器

JQuery通過選取HTML元素來對其進行操作;語法:$(selector).action()

  • selector:選擇器,結合XPath語法和CSS選擇器語法
  • action():對元素的操作

四、事件

4.1 概述

常見DOM事件:

鼠標事件鍵盤事件表單事件文檔/窗口事件
clickkeypresssubmitload
dblclickkeydownchangeresize
mouseenterkeyupfocusscroll
mouseleaveblurunload
hover

事件對象的關鍵屬性和方法:

4.2 事件綁定/解綁

????????????????綁定事件???????????????

on方法用來綁定一個或多個事件處理函數,格式為on(events[,selector][,data],handler)

events:事件

selector:選擇器,用于篩選觸發事件的選定元素后代

data:數據,觸發事件時用來傳遞給處理程序

handler:處理器,觸發事件時要執行的函數

// 單個事件
$("button").on("click", function() {console.log("按鈕被點擊");
});// 多個事件(共用處理函數)
$("input").on("focus blur", function(e) {console.log(e.type); // 輸出 'focus' 或 'blur'
});// 多個事件和多個處理
$( "div" ).on({mouseenter: function() {console.log( "hovered over a div" );},mouseleave: function() {console.log( "mouse left a div" );},click: function() {console.log( "clicked on a div" );}
});// 將數據傳遞給處理程序
$( "button" ).on( "click", {name: "Karl"
}, function greet( event ) {alert( "Hello " + event.data.name );
} );

除此之外,也可以使用快捷方法:

方法描述
click()按鈕點擊時觸發事件
dblclick()雙擊元素時觸發事件
mouseenter()鼠標穿過元素時觸發事件
mouseleave()鼠標離開元素時觸發事件
mousedown()鼠標移動到元素上方,并按下按鍵時觸發事件
mouseup()在元素上方松開鼠標按鈕時觸發事件
hover()懸停在元素上方時觸發事件
focus()元素獲得焦點觸發事件
blur()元素失去焦點觸發事件

click方法:按鈕點擊時會觸發事件

<html><head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$( window ).on( "load", function() {$( "#btn" ).click(function() {alert( "按鈕被點擊了!" );})});</script></head><body><p>Hello world!</p><button class="btn" id="btn">Click me!</button></body>
</html>

dblclick方法:雙擊元素時,會觸發事件

<html><head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$( window ).on( "load", function() {$( "p" ).dblclick(function() {alert( "文本被雙擊了!" );})$( "#btn" ).click(function() {alert( "按鈕被單擊了!" );})});</script></head><body><p>Hello world!</p><button class="btn" id="btn">Click me!</button></body>
</html>

mouseenter方法:鼠標穿過元素時,會觸發事件

<html><head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$( window ).on( "load", function() {$( "p" ).mouseenter(function() {alert( "文本被穿過了!" );})});</script></head><body><p>Hello world!</p></body>
</html>

????????????????事件解綁???????????????

通過使用off方法來移除通過on方法綁定的事件

// 移除所有事件
$("button").off();// 移除特定類型事件
$("input").off("focus");// 移除委托事件
$("#container").off("click", ".dynamic-item");

4.3 一次性事件

one方法只執行一次后就會自動解綁:

// 單個事件
$("button").one("click", function() {alert("僅顯示一次");
});// 多個事件
$("input").one("focus blur", function(e) {console.log(e.type); 
});

4.4 事件委托

事件委托利用事件冒泡,將子元素的事件處理程序綁定到父元素上,這樣可以通過父元素統一管理子元素的事件處理程序。

在父元素上監聽子元素事件,格式為父元素.on(事件,子選擇器,處理函數)

$( "#list" ).on( "click", "a", function( event ) {event.preventDefault();console.log( $( this ).text() );
});

事件委托與直接綁定的區別:

4.5 自定義事件

trigger方法可以手動觸發元素上的事件,包括自定義事件,語法為trigger(eventType[,extraParameters])或者trigger(event[,extraParameters])

eventType:事件類型

extraParameters:傳遞給事件處理程序的其他參數

<html lang="en">
<head><meta charset="utf-8"><style>button {margin: 10px;}div {color: blue;font-weight: bold;}span {color: red;}</style><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body><button>Button #1</button><button>Button #2</button><div><span>0</span> button #1 clicks.</div><div><span>0</span> button #2 clicks.</div><script>$( "button" ).first().on( "click", function() {update( $( "span" ).first() );} );$( "button" ).last().on( "click", function() {$( "button" ).first().trigger( "click" );update( $( "span" ).last() );} );function update( j ) {var n = parseInt( j.text(), 10 );j.text( n + 1 );}</script>
</body>
</html>

在這里插入圖片描述

五、效果

5.1 隱藏/顯示

通過hide(speed,callback)方法來隱藏HTML元素,show(speed,callback)方法來顯示HTML元素

speed:隱藏/顯示的速度,可取slow、fast或者毫秒數

callback:隱藏/顯示完成后所執行的函數

<html><head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$( window ).on( "load", function() {$( "button.hide" ).click(function() {$( "p" ).hide();});$( "button.show" ).click(function() {$( "p" ).show();});});</script></head><body><p >Hello world!</p><button class="hide">Hide</button><button class="show">Show</button></body>
</html>

在這里插入圖片描述

使用speed參數來看一下效果:

speed參數效果
speed=1000在這里插入圖片描述
speed="fast"在這里插入圖片描述
speed="slow"在這里插入圖片描述

除此之外,也可以使用toggle(speed,callback)方法來切換hide()和show()方法:

<html><head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$( window ).on( "load", function() {$( "button.hide" ).click(function() {$( "p" ).hide(speed="slow");});$( "button.show" ).click(function() {$( "p" ).show(speed="slow");});$( "button.toggle" ).click(function() {$( "p" ).toggle(speed="slow"); })});</script></head><body><p >Hello world!</p><button class="hide">Hide</button><button class="show">Show</button><button class="toggle">Toggle</button></body>
</html>

在這里插入圖片描述

5.2 淡入淡出

fadeIn(speed,callback)方法來實現淡入已隱藏的元素

<html><head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$( window ).on( "load", function() {$( "button.hide" ).click(function() {$( "p" ).hide(speed="slow");});$( "button.FadeIn" ).click(function() {$( "p" ).fadeIn(speed="slow");});});</script></head><body><p >Hello world!</p><button class="FadeIn">FadeIn</button><button class="hide">Hide</button></body>
</html>

在這里插入圖片描述

fadeOut(speed,callback)用于淡出可見元素

<html><head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$( window ).on( "load", function() {$( "button.FadeOut" ).click(function() {$( "p" ).fadeOut(speed="slow");});});</script></head><body><p >Hello world!</p><button class="FadeOut">FadeOut</button></body>
</html>

在這里插入圖片描述

fadeToggle(speed,callback)方法可以在fadeIn方法和fadeOut方法切換

<html><head><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$( window ).on( "load", function() {$( "button.FadeToggle" ).click(function() {$( "p" ).fadeToggle(speed="slow");});});</script></head><body><p >Hello world!</p><button class="FadeToggle">FadeToggle</button></body>
</html>

在這里插入圖片描述

fadeTo(speed,opacity,callback)允許漸變為給定的不透明度

opacity:將淡入淡出效果設置為給定的不透明度,值介于0到1之間

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("#div1").fadeTo("slow",0.15);$("#div2").fadeTo("slow",0.4);$("#div3").fadeTo("slow",0.7);});});</script></head><body><button>點我讓顏色變淡</button><br><br><div id="div1" style="width:80px;height:80px;background-color:red;"></div><br><div id="div2" style="width:80px;height:80px;background-color:green;"></div><br><div id="div3" style="width:80px;height:80px;background-color:blue;"></div></body>
</html>

在這里插入圖片描述

5.3 滑動

slideDown(speed,callback)用于向下滑動元素

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".SlideDown").click(function(){$("#div1").slideDown(1000);});$("#div1").hide(); // 初始隱藏div1});</script></head><body><button class ="SlideDown">點擊這里,向下滑動圖片</button><div id="div1" style="background-color:red;height:100px;width:300px;"></body>
</html>

在這里插入圖片描述

slideUp(speed,callback)向上滑動元素

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".SlideUp").click(function(){$("#div1").slideUp(1000);});});</script></head><body><button class ="SlideUp">點擊這里,向上滑動圖片</button><div id="div1" style="background-color:red;height:100px;width:300px;"></body>
</html>

在這里插入圖片描述

slideToggle(speed,callback)可以在slideDown方法和slideUp方法之間進行切換

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".SlideToggle").click(function(){$("#div1").slideToggle(1000);});});</script></head><body><button class ="SlideToggle">點擊這里,滑動圖片</button><div id="div1" style="background-color:red;height:100px;width:300px;"></body>
</html>

在這里插入圖片描述

5.4 動畫

animate({params},speed,callback)用于創建自定義動畫

{params}:定義形成動畫的CSS屬性,屬性名使用Camel標記法,例如使用paddingLeft而不是padding-left,同時顏色動畫需要從官網下載。

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".animate").click(function(){$("#div1").animate({width:'100px'}, 1000);});});</script></head><body><button class ="animate">動畫實現</button><div id="div1" style="background-color:red;height:100px;width:300px;"></body>
</html>

在這里插入圖片描述

{params}也可以使用相對值(相對于元素的當前值),有+=或者-=

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".animate").click(function(){$("#div1").animate({width:'+=100px'}, 1000, function(){// 動畫完成時的回調函數$("#div1").animate({width:'300px'}, 1000);});});});</script></head><body><button class ="animate">動畫實現</button><div id="div1" style="background-color:red;height:100px;width:300px;"></body>
</html>

在這里插入圖片描述

{params}也可以使用預定義的值,有showhidetoggle

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".animate").click(function(){$("#div1").animate({height:'toggle'}, 1000);});});</script></head><body><button class ="animate">動畫實現</button><div id="div1" style="background-color:red;height:100px;width:300px;"></body>
</html>

在這里插入圖片描述

animate方法可以使用隊列功能,對同一個元素可以應用多個動畫,按順序執行

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".animate").click(function(){$("#div1").animate({height:'toggle'}, 1000);$("#div1").animate({width:'toggle'}, 1000);$("#div1").animate({opacity:'0.4'}, 1000);});});</script></head><body><button class ="animate">動畫實現</button><div id="div1" style="background-color:red;height:100px;width:300px;"></body>
</html>

在這里插入圖片描述

停止動畫:stop(stopAll,goToEnd)

stopAll:是否應該清除動畫隊列,默認為False,僅停止當前活動的動畫,接著執行隊列中下一個動畫。

goToEnd:是否立即完成當前動畫,默認為False

參數效果
stop(true,true)在這里插入圖片描述
stop(true,false)在這里插入圖片描述
stop(false,true)在這里插入圖片描述
stop(false,false)在這里插入圖片描述

六、鏈

允許在一條語句里對同一個元素運行多個jQuery方法

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

七、HTML

7.1 內容/屬性

text([text])方法用來設置或返回所選元素的文本內容

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".set").click(function(){$("#div1 p").text("<b>hello world</b>");});$(".show").click(function(){alert($("#div1 p").text());})});</script></head><body><div id="div1"><p>hello</p></div><div id="div2"><p>world</p></div><button class="set">set text</button><button class="show">show text</button></body>
</html>

在這里插入圖片描述

html([htmlString])方法不適用于XML文檔,可以設置或返回所選元素的內容,包括HTML標簽

在這里插入圖片描述

val([value])方法主要用于獲取和設置表單元素

在這里插入圖片描述

獲取屬性: attr()方法,相比于prop方法他可以獲取自定義屬性

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".show").click(function(){alert($("#link1").attr("href"));})$(".set").click(function(){$("#link1").attr("href","http://www.sina.com.cn");alert($("#link1").attr("href")); })});</script></head><body><div id="div1"><a href="http://www.baidu.com" id="link1">hello</a></div><button class="show">show href</button><button class="set">set href</button></body>
</html>

在這里插入圖片描述

7.2 元素操作

方法一覽描述
append(content[,content])在被選元素的結尾插入內容
prepend(content[,content])在被選元素的開頭插入內容
after(content[,content])在被選元素之后插入內容
before(content[,content])在被選元素之前插入內容
remove([selector])刪除被選元素及其子元素
selector可以過濾被刪除的元素
empty從被選元素中刪除子元素

????????????????增加元素???????????????

append、prepend、before、after方法的用法都相似,只不過插入的位置不同

以append方法舉例,append方法將指定內容作為每個元素的最后一個子元素插入

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".set").click(function(){$(".inner").append("<p>world</p>");})});</script></head><body><h2>Greetings</h2><div class="container"><div class="inner">Hello</div><div class="inner">Goodbye</div></div><button class="set">set</button></body>
</html>

在這里插入圖片描述

除此之外append方法也會將HTML屬性附加到段落上:

<html><head><meta charset="utf-8"><style>.inner{background-color: yellow;}</style></style><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$(".set").click(function(){$(".inner").append("<p>world</p>");})});</script></head><body><h2>Greetings</h2><div class="container"><div class="inner">Hello world</div></div><button class="set">set</button></body>
</html>

在這里插入圖片描述

????????????????刪除元素???????????????

empty方法和remove方法的區別:

remove方法將刪除與元素關聯的所有綁定事件和jQuery數據

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script></script><script>$(document).ready(function(){$("button").click(function(){$("#div1").remove();});});</script></head><body><div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">這是 div 中的一些文本。<p>這是在 div 中的一個段落。</p><p>這是在 div 中的另外一個段落。</p></div><br><button>清空div元素</button></body>
</html>

在這里插入圖片描述

可以將選擇器作為可選參數包含在內,效果都是一樣的:

<script>$(document).ready(function(){$("button").click(function(){$("div").remove("div#div1");});});
</script>

而empty方法會移除目標元素所有子節點,但不會刪除目標元素本身和它綁定的數據或事件

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script></script><script>$(document).ready(function(){$("button").click(function(){$("#div1").empty();});});</script></head><body><div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">這是 div 中的一些文本。<p>這是在 div 中的一個段落。</p><p>這是在 div 中的另外一個段落。</p></div><br><button>清空div元素</button></body>
</html>

在這里插入圖片描述

7.3 類屬性

方法一覽描述
addClass(className)向被選元素添加一個或多個類
removeClass(className)從被選元素刪除一個或多個類
toggleClass(className)對被選元素進行添加/刪除類的切換操作

介紹一下addClass、removeClass、toggleClass的使用:

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script></script><script>$(document).ready(function(){$(".add").click(function(){$("h1,h2,p").addClass("b");$("div").addClass("a");});$(".remove").click(function(){$("h1,h2,p").removeClass("b");$("div").removeClass("a");});$(".toggle").click(function(){$("h1,h2,p").toggleClass("b");$("div").toggleClass("a");})});</script><style type="text/css">.a{font-weight:bold;font-size:xx-large;}.b{color:palevioletred;}</style></head><body><h1>標題 1</h1><h2>標題 2</h2><p>這是一個段落。</p><p>這是另外一個段落。</p><div>這是一些重要的文本!</div><br><button class="add">添加 class</button><button class="remove">移除 class</button><button class="toggle">切換 class</button></body>
</html>

在這里插入圖片描述

7.4 樣式屬性

????????????????css樣式???????????????

css方法可以返回或者設置元素的一個或多個樣式

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script></script><script>$(document).ready(function(){$(".set").click(function(){$("div").css("color","yellow");});$(".show").click(function(){alert($("div").css("color"));})});</script><style type="text/css">.a{font-weight:bold;font-size:xx-large;color: blueviolet;}.b{color:palevioletred;}</style></head><body><h1 class="b">標題 1</h1><div class="a">這是一些重要的文本!</div><br><button class="set">設置顏色</button><button class="show">展示顏色</button></body>
</html>

在這里插入圖片描述

????????????????尺寸?????????????????

jQuery尺寸大致布局:

方法一覽描述
width(width)設置或返回元素的寬度,不包括內邊距、邊框或外邊距
height(height)設置或返回元素的高度,不包括內邊距、邊框或外邊距
innerWidth返回元素上的寬度,包括內邊距
innerHeight返回元素上的高度,包括內邊距
outerWidth返回元素上的寬度,包括內邊距和邊框
outerHeight返回元素上的高度,包括內邊距和邊框

width和height的參數值可以設置為數字、父元素寬度的比例、回調函數。

$("#element").width(200);        // 設置為 200px
$("#element").width("50%");       // 設置為父元素寬度的 50%
$("#element").width(function(index, currentWidth) {return currentWidth + 10;       // 動態調整寬度
});

除此之外,還可以獲取窗口與文檔的尺寸

  1. 窗口尺寸:$(window).width()$(window).height()
  2. 文檔尺寸:$(document).width()$(document).height()

總結一下:

????????????????坐標?????????????????

offset和position方法的區別:

offset獲取第一個匹配元素的當前坐標或者設置匹配元素相對于文檔的坐標

獲取元素位置時,他會返回包含top和left屬性的對象

const position = $("#myElement").offset();
console.log("Top:", position.top, "Left:", position.left);
// 輸出結果:Top: 200, Left: 300

設置元素位置時,格式為offset({top:value,left:value})

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script></script><script>$(document).ready(function(){$(".set").click(function(){$("div").offset({top: 20,left: 50})
});});</script><style type="text/css">.a{font-weight:bold;font-size:xx-large;color: blueviolet;}</style></head><body><div class="a">這是一些重要的文本!</div><br><button class="set">設置位置</button></body>
</html>

在這里插入圖片描述

7.5 遍歷dom

????????????????遍歷祖先????????????????

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script></script><script>$(document).ready(function(){$(".show-parent").click(function(){$(".child").parent().css("background-color","rgb(255, 0, 0)");});$(".show-parents").click(function(){$(".child").parents().css("background-color","rgb(3, 255, 0)");}); $(".show-parentuntil").click(function(){$(".child").parentsUntil(".a").css("background-color","#67cdf8");});});  </script><style>.a{width: 500px;height: 500px;background-color: rgb(163, 221, 246);}.parent{width: 200px;height: 200px;background-color: rgb(242, 133, 133);}.parents{width: 300px;height: 300px;background-color: rgb(140, 218, 140); }.child{width: 100px;height: 100px;background-color: rgb(156, 156, 241);}</style></head><body><div class="a"><div class ="parents"><div class="parent"><span class="child">haha</span><p></p></div></div></div><button class="show-parent">展示parent</button><button class="show-parents">展示parents</button><button class="show-parentuntil">展示parentuntil</button></body>
</html>

在這里插入圖片描述

????????????????遍歷后代????????????????

????????????????遍歷同胞????????????????

<html><head><meta charset="utf-8"><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script></script><script>$(document).ready(function(){$(".menu-header").click(function(){$(this).next(".menu-content").slideToggle();$(this).toggleClass("active");});  });</script><style>.menu {width: 250px;font-family: Arial, sans-serif;}.menu-title {padding: 10px;background-color: #2c3e50;color: white;font-weight: bold;margin-top: 5px;}.menu-header {padding: 8px 10px;background-color: #3498db;color: white;cursor: pointer;position: relative;}.menu-header:after {content: "+";position: absolute;right: 10px;}.menu-header.active:after {content: "-";}.menu-content {display: none;padding: 5px 0;background-color: #f9f9f9;}.sub-item {padding: 6px 15px;color: #333;}.sub-item:hover {background-color: #e0e0e0;}</style></head><body><div class="menu"><div class="menu-title">主菜單</div><div class="menu-item"><div class="menu-header">菜單項1</div><div class="menu-content"><div class="sub-item">子項1-1</div><div class="sub-item">子項1-2</div></div></div><div class="menu-item"><div class="menu-header">菜單項2</div><div class="menu-content"><div class="sub-item">子項2-1</div><div class="sub-item">子項2-2</div><div class="sub-item">子項2-3</div></div></div><div class="menu-title">其他菜單</div><div class="menu-item"><div class="menu-header">菜單項3</div><div class="menu-content"><div class="sub-item">子項3-1</div></div></div></div></body>
</html>

在這里插入圖片描述

7.6 過濾元素

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/76207.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/76207.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/76207.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

module錯誤集合

Library projects cannot set applicationId. applicationId is set to com.example.mylogin in default 在導入一個項目時&#xff0c;提示“Error:Library projects cannot set applicationId. applicationId is set to ‘com.xxx.yyy’ in default config.”&#xff0c;顯…

Spring Cloud 通用相關組件詳解

前言 Spring Cloud 是一個基于 Spring Boot 的微服務開發框架&#xff0c;它為開發者提供了一套完整的工具和組件&#xff0c;用于快速構建分布式系統中的常見模式&#xff08;如服務注冊與發現、負載均衡、配置管理等&#xff09;。本文將詳細介紹 Spring Cloud 的通用組件&a…

BUUCTF-web刷題篇(19)

28.CheckIn 源碼&#xff1a; #index.php <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><meta http-equiv&q…

如何在Android系統上單編ko?

文章目錄 一、先了解編譯驅動需要什么&#xff1f;二、配置makefile1、在Android系統編譯LOG上找到編譯器信息&#xff08;一般都會打印出來&#xff09;2、基于源MK構造 可獨立運行的makefile3&#xff09;進入docker&#xff0c;在此makefile目錄下敲make4&#xff09;最后根…

【Pandas】pandas DataFrame to_numpy

Pandas2.2 DataFrame Conversion 方法描述DataFrame.astype(dtype[, copy, errors])用于將 DataFrame 中的數據轉換為指定的數據類型DataFrame.convert_dtypes([infer_objects, …])用于將 DataFrame 中的數據類型轉換為更合適的類型DataFrame.infer_objects([copy])用于嘗試…

2025常用的ETL 產品推薦:助力企業激活數據價值

在當今數字化時代&#xff0c;企業面臨著海量數據的挑戰與機遇&#xff0c;ETL&#xff08;Extract, Transform, Load&#xff09;工具作為數據整合與分析的關鍵環節&#xff0c;其重要性日益凸顯。ETL 廠商眾多&#xff0c;各有優勢&#xff0c;本文將從多個維度進行分析&…

LeetCode算法題(Go語言實現)_37

題目 給你一棵以 root 為根的二叉樹&#xff0c;二叉樹中的交錯路徑定義如下&#xff1a; 選擇二叉樹中 任意 節點和一個方向&#xff08;左或者右&#xff09;。 如果前進方向為右&#xff0c;那么移動到當前節點的的右子節點&#xff0c;否則移動到它的左子節點。 改變前進方…

博途 TIA Portal之1200做從站與匯川EASY的TCP通訊

上篇我們寫到了博途做主站與匯川EASY的通訊。通訊操作起來很簡單,當然所謂的簡單,也是相對的,如果操作成功一次,那么后面就很容易了, 如果操作不成功,就會很遭心。本篇我們將1200做從站,與匯川EASY做主站進行TCP的通訊。 1、硬件準備 1200PLC一臺,帶調試助手的PC機一…

Mysql(繼續更新)

INnoDB 三特性 事務 外鍵 行級鎖(開啟事務時,查詢后加FOR UPDATE) MySQL 使用 InnoDB&#xff0c;在 默認隔離級別 —— REPEATABLE READ&#xff08;可重復讀&#xff09; 下 開啟事務&#xff0c;執行 UPDATE 時默認會加行鎖 只要事務沒有提交 這條數據會鎖住 …

[IOI 1994] 數字三角形 Number Triangles

題目鏈接 思路&#xff08;上到下&#xff09;&#xff1a; ①從上往下遞推&#xff1a; f[i][j] max(f[i-1][j] g[i][j], f[i-1][j-1]g[i][j]) ②對最后一層&#xff0c;遍歷一下&#xff0c;找到最大的答案。 代碼&#xff08;上到下&#xff09;&#xff1a; #inclu…

基于Qt的串口通信工具

程序介紹 該程序是一個基于Qt的串口通信工具&#xff0c;專用于ESP8266 WiFi模塊的AT指令配置與調試。主要功能包括&#xff1a; 1. 核心功能 串口通信&#xff1a;支持串口開關、參數配置&#xff08;波特率、數據位、停止位、校驗位&#xff09;及數據收發。 AT指令操作&a…

第5篇:Linux程序訪問控制FPGA端LEDR<三>

Q&#xff1a;如何具體設計.c程序代碼訪問控制FPGA端外設&#xff1f; A&#xff1a;以控制DE1-SoC開發板的LEDR為例的Linux .C程序代碼。頭文件fcntl.h和sys/mman.h用于使用/dev/mem文件&#xff0c;以及mmap和munmap內核函數&#xff1b;address_map_arm.h指定了DE1-SoC_Com…

【學生管理系統升級版】

學生管理系統升級版 需求分析&#xff1a;注冊功能:登錄功能&#xff1a;驗證碼規則&#xff1a;忘記密碼&#xff1a; 實操&#xff1a;系統主頁面注冊功能登錄功能忘記密碼效果演示 需求 為學生管理系統書寫一個登陸、注冊、忘記密碼的功能。 ? ? 只有用戶登錄成功之后&…

CSS Grid布局:從入門到放棄再到真香

Flexbox 與 Grid 布局&#xff1a;基礎概念與特點 Flexbox Flexbox&#xff08;Flexible Box Layout&#xff09;&#xff0c;即彈性盒布局模型&#xff0c;主要用于創建一維布局&#xff0c;能夠輕松實現元素在一行或一列中的排列、對齊與分布。通過display: flex屬性啟用 Fl…

C++怎么調用類中的函數

1. 棧上對象 調用普通成員方法 普通成員方法需要通過類的對象實例&#xff08;或指針、引用&#xff09;來調用。 示例&#xff1a; class MyClass { public:void normalMethod() {std::cout << "普通成員方法被調用" << std::endl;} };int main() {M…

go游戲后端開發31:麻將游戲的碰牌與胡牌邏輯

以下是潤色后的版本&#xff1a; 1. 碰牌邏輯 1.1 觸發碰牌 當一個玩家棄牌后&#xff0c;其他玩家可以選擇碰牌。如果當前玩家決定碰牌&#xff0c;系統需要通知所有玩家這一操作。碰牌操作完成后&#xff0c;當前玩家需要出一張牌&#xff0c;系統同樣需要通知所有玩家。 …

十分鐘機器學習之--------------線性回歸

線性回歸&#xff08;linear regression&#xff09;是一種基于數學模型的算法&#xff0c;首先假設數據集與標簽之間存在線性關系&#xff0c;然后簡歷線性模型求解參數。在實際生活中&#xff0c;線性回歸算法因為其簡單容易計算&#xff0c;在統計學經濟學等領域都有廣泛的應…

學透Spring Boot — 017. 處理靜態文件

這是我的《學透Spring Boot》專欄的第17篇文章&#xff0c;了解更多內容請移步我的專欄&#xff1a; Postnull CSDN 學透 Spring Boot 目錄 靜態文件 靜態文件的默認位置 通過配置文件配置路徑 通過代碼配置路徑 靜態文件的自動配置 總結 靜態文件 以前的傳統MVC的項目…

深入理解 JavaScript 數組查找:如何高效獲取特定元素

深入理解 JavaScript 數組查找&#xff1a;如何高效獲取特定元素 深入理解 JavaScript 數組查找&#xff1a;如何高效獲取特定元素引言問題場景解決方案1. 使用 Array.prototype.find()2. 處理 Proxy 對象的情況3. 備選方案&#xff1a;Array.prototype.filter()4. 傳統 for 循…

HTML5+CSS3小實例:純CSS繪制七巧板

實例:純CSS繪制七巧板 技術棧:HTML+CSS 效果: 源碼: 【HTML】 <!DOCTYPE html> <html lang="zh-CN"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale…