ç®å½
- jQueryä»ç»
- jQueryçä¼å¿
- jQueryå 容
- jQueryçæ¬
- jQuery对象
- jQueryåºç¡è¯æ³
- æ¥æ¾æ ç¾
- çéå¨æ¹æ³
- æä½æ ç¾
- äºä»¶
- å¨ç»ææ
- è¡¥å
1ï¼ jQueryä»ç»
- jQueryæ¯ä¸ä¸ªè½»é级çãå ¼å®¹å¤æµè§å¨çJavaScriptåºã
- jQuery使ç¨æ·è½å¤æ´æ¹ä¾¿å°å¤çHTML DocumentãEventsãå®ç°å¨ç»ææãæ¹ä¾¿å°è¿è¡Ajax交äºï¼è½å¤æ大å°ç®åJavaScriptç¼ç¨ãå®çå®æ¨å°±æ¯ï¼âWrite less, do more.â
2ï¼ jQueryçä¼å¿
- ä¸æ¬¾è½»é级çJSæ¡æ¶ãjQueryæ ¸å¿jsæ件æå åkbï¼ä¸ä¼å½±å页é¢å è½½é度ã
- 丰å¯çDOMéæ©å¨,jQueryçéæ©å¨ç¨èµ·æ¥å¾æ¹ä¾¿ï¼æ¯å¦è¦æ¾å°æ个DOM对象çç¸é»å ç´ ï¼JSå¯è½è¦å好å è¡ä»£ç ï¼èjQueryä¸è¡ä»£ç å°±æå®äºï¼åæ¯å¦è¦å°ä¸ä¸ªè¡¨æ ¼çéè¡åè²ï¼jQueryä¹æ¯ä¸è¡ä»£ç æå®ã
- é¾å¼è¡¨è¾¾å¼ãjQueryçé¾å¼æä½å¯ä»¥æå¤ä¸ªæä½åå¨ä¸è¡ä»£ç éï¼æ´å ç®æ´ã
- äºä»¶ãæ ·å¼ãå¨ç»æ¯æãjQueryè¿ç®åäºjsæä½cssç代ç ï¼å¹¶ä¸ä»£ç çå¯è¯»æ§ä¹æ¯jsè¦å¼ºã
- Ajaxæä½æ¯æãjQueryç®åäºAJAXæä½ï¼å端åªéè¿åä¸ä¸ªJSONæ ¼å¼çå符串就è½å®æä¸å端çéä¿¡ã
- è·¨æµè§å¨å ¼å®¹ãjQueryåºæ¬å ¼å®¹äºç°å¨ä¸»æµçæµè§å¨ï¼ä¸ç¨å为æµè§å¨çå ¼å®¹é®é¢è伤éèçã
- æ件æ©å±å¼åãjQueryæç丰å¯ç第ä¸æ¹çæ件ï¼ä¾å¦ï¼æ å½¢èåãæ¥ææ§ä»¶ãå¾çåæ¢æ件ãå¼¹åºçªå£ççåºæ¬å端页é¢ä¸çç»ä»¶é½æ对åºæ件ï¼å¹¶ä¸ç¨jQueryæ件ååºæ¥çææå¾ç«ï¼å¹¶ä¸å¯ä»¥æ ¹æ®èªå·±éè¦å»æ¹ååå°è£ æ件ï¼ç®åå®ç¨ã
3ï¼ jQueryå 容
- 1 éæ©å¨
- 2 çéå¨
- 3 æ ·å¼æä½
- 4 ææ¬æä½
- 5 å±æ§æä½
- 6 ææ¡£å¤ç
- 7 äºä»¶
- 8 å¨ç»ææ
- 9 æ件
- 10 eachãdataãAjax
- ä¸è½½é¾æ¥ï¼https://jquery.com/
- ä¸æææ¡£ï¼http://jquery.cuishifeng.cn/
4ï¼ jQueryçæ¬
- 1.xï¼å ¼å®¹IE678,使ç¨æ为广æ³çï¼å®æ¹åªåBUGç»´æ¤ï¼åè½ä¸åæ°å¢ãå æ¤ä¸è¬é¡¹ç®æ¥è¯´ï¼ä½¿ç¨1.xçæ¬å°±å¯ä»¥äºï¼æç»çæ¬ï¼1.12.4 (2016å¹´5æ20æ¥)
- 2.xï¼ä¸å ¼å®¹IE678ï¼å¾å°æ人使ç¨ï¼å®æ¹åªåBUGç»´æ¤ï¼åè½ä¸åæ°å¢ãå¦æä¸èèå ¼å®¹ä½çæ¬çæµè§å¨å¯ä»¥ä½¿ç¨2.xï¼æç»çæ¬ï¼2.2.4 (2016å¹´5æ20æ¥)
- 3.xï¼ä¸å ¼å®¹IE678ï¼åªæ¯æææ°çæµè§å¨ãéè¦æ³¨æçæ¯å¾å¤èçjQueryæ件ä¸æ¯æ3.xçãç®å该çæ¬æ¯å®æ¹ä¸»è¦æ´æ°ç»´æ¤ççæ¬ã
- ç»´æ¤IE678æ¯ä¸ä»¶è®©äººå¤´ç¼çäºæ ï¼ä¸è¬æ们é½ä¼é¢å¤å è½½ä¸ä¸ªCSSåJSåç¬å¤çãå¼å¾åºå¹¸çæ¯ä½¿ç¨è¿äºæµè§å¨ç人ä¹éæ¥åå°ï¼PC端ç¨æ·å·²ç»éæ¥è¢«ç§»å¨ç«¯ç¨æ·æå代ï¼å¦æ没æç¹æ®è¦æ±çè¯ï¼ä¸è¬é½ä¼éæ©æ¾å¼å¯¹678çæ¯æã
5ï¼ jQuery对象
- jQuery对象就æ¯éè¿jQueryå
è£
DOM对象å产çç对象ãjQueryå¯¹è±¡æ¯ jQueryç¬æçãå¦æä¸ä¸ªå¯¹è±¡æ¯ jQuery对象ï¼é£ä¹å®å°±å¯ä»¥ä½¿ç¨jQueryéçæ¹æ³ï¼ä¾å¦
$(â#i1â).html()
ã -
$("#i1").html()
çæææ¯ï¼è·åidå¼ä¸º i1çå ç´ çhtml代ç ãå ¶ä¸ html()æ¯jQueryéçæ¹æ³ã - ç¸å½äºï¼
document.getElementById("i1").innerHTML;
- è½ç¶ jQuery对象æ¯å è£ DOM对象å产ççï¼ä½æ¯ jQuery对象æ æ³ä½¿ç¨ DOM对象çä»»ä½æ¹æ³ï¼åç DOM对象ä¹æ²¡ä¸è½ä½¿ç¨ jQueryéçæ¹æ³ã
- ä¸ä¸ªçº¦å®ï¼æ们å¨å£°æä¸ä¸ªjQuery对象åéçæ¶åå¨åéååé¢å ä¸$ï¼
var $variable = jQuery对å
var variable = DOM对象
$variable[0]//jQuery对象转æDOM对象
- æ¿ä¸é¢é£ä¸ªä¾å举ä¾ï¼jQuery对象åDOM对象ç使ç¨ï¼
$("#i1").html();//jQuery对象å¯ä»¥ä½¿ç¨jQueryçæ¹æ³
$("#i1")[0].innerHTML;// DOM对象使ç¨DOMçæ¹æ³
6ï¼ jQueryåºç¡è¯æ³
$(selector).action()
7ï¼ æ¥æ¾æ ç¾
7.1 åºæ¬éæ©å¨
- idéæ©å¨ï¼
$("#id")
- æ ç¾éæ©å¨ï¼
$("tagName")
- classéæ©å¨ï¼
$(".className")
- é å使ç¨ï¼
$("div.c1") // æ¾å°æc1 classç±»çdivæ ç¾
- ææå ç´ éæ©å¨ï¼
$("*")
- ç»åéæ©å¨ï¼
$("#id, .className, tagName")
7.2 å±çº§éæ©å¨
- xåyå¯ä»¥ä¸ºä»»æéæ©å¨
$("x y");// xçææå代yï¼ååååï¼
$("x > y");// xçææå¿åyï¼å¿åï¼
$("x + y")// æ¾å°ææç´§æ¨å¨xåé¢çy
$("x ~ y")// xä¹åææçå
å¼y
7.3 åºæ¬çéå¨
:first // 第ä¸ä¸ª
:last // æåä¸ä¸ª
:eq(index)// ç´¢å¼çäºindexçé£ä¸ªå
ç´
:even // å¹é
ææç´¢å¼å¼ä¸ºå¶æ°çå
ç´ ï¼ä» 0 å¼å§è®¡æ°
:odd // å¹é
ææç´¢å¼å¼ä¸ºå¥æ°çå
ç´ ï¼ä» 0 å¼å§è®¡æ°
:gt(index)// å¹é
ææ大äºç»å®ç´¢å¼å¼çå
ç´
:lt(index)// å¹é
ææå°äºç»å®ç´¢å¼å¼çå
ç´
:not(å
ç´ éæ©å¨)// 移é¤ææ满足notæ¡ä»¶çæ ç¾
:has(å
ç´ éæ©å¨)// éåææå
å«ä¸ä¸ªæå¤ä¸ªæ ç¾å¨å
¶å
çæ ç¾(æçæ¯ä»å代å
ç´ æ¾)
- ä¾å
$("div:has(h1)")// æ¾å°ææå代ä¸æh1æ ç¾çdivæ ç¾
$("div:has(.c1)")// æ¾å°ææå代ä¸æc1æ ·å¼ç±»çdivæ ç¾
$("li:not(.c1)")// æ¾å°ææä¸å
å«c1æ ·å¼ç±»çliæ ç¾
$("li:not(:has(a))")// æ¾å°ææå代ä¸ä¸å«aæ ç¾çliæ ç¾
- èªå®ä¹æ¨¡ææ¡ï¼ä½¿ç¨jQueryå®ç°å¼¹åºåéèåè½
- jQueryçèªå®ä¹æ¨¡ææ¡
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>èªå®ä¹æ¨¡ææ¡</title>
<style>
.cover {
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: darkgrey;
z-index: 999;
}
.modal {
width: 600px;
height: 400px;
background-color: white;
position: fixed;
left: 50%;
top: 50%;
margin-left: -300px;
margin-top: -200px;
z-index: 1000;
}
.hide {
display: none;
}
</style>
</head>
<body>
<input type="button" value="å¼¹" id="i0">
<div class="cover hide"></div>
<div class="modal hide">
<label for="i1">å§å</label>
<input id="i1" type="text">
<label for="i2">ç±å¥½</label>
<input id="i2" type="text">
<input type="button" id="i3" value="å
³é">
</div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
var tButton = $("#i0")[0];
tButton.onclick=function () {
var coverEle = $(".cover")[0];
var modalEle = $(".modal")[0];
$(coverEle).removeClass("hide");
$(modalEle).removeClass("hide");
};
var cButton = $("#i3")[0];
cButton.onclick=function () {
var coverEle = $(".cover")[0];
var modalEle = $(".modal")[0];
$(coverEle).addClass("hide");
$(modalEle).addClass("hide");
}
</script>
</body>
</html>
7.4 å±æ§éæ©å¨
[attribute]
[attribute=value]// å±æ§çäº
[attribute!=value]// å±æ§ä¸çäº
- ä¾å
// 示ä¾
<input type="text">
<input type="password">
<input type="checkbox">
$("input[type='checkbox']");// åå°checkboxç±»åçinputæ ç¾
$("input[type!='text']");// åå°ç±»åä¸æ¯textçinputæ ç¾
7.5 表åçéå¨
:text
:password
:file
:radio
:checkbox
:submit
:reset
:button
- ä¾å
$(":checkbox") // æ¾å°ææçcheckbox
- 表å对象å±æ§:
:enabled
:disabled
:checked
:selected
ä¾åï¼
æ¾å°å¯ç¨çinputæ ç¾
<form>
<input name="email" disabled="disabled" />
<input name="id" />
</form>
$("input:enabled") // æ¾å°å¯ç¨çinputæ ç¾
- æ¾å°è¢«éä¸çoptionï¼
<select id="s1">
<option value="beijing">å京å¸</option>
<option value="shanghai">ä¸æµ·å¸</option>
<option selected value="guangzhou">广å·å¸</option>
<option value="shenzhen">æ·±å³å¸</option>
</select>
$(":selected") // æ¾å°ææ被éä¸çoption
8ï¼ çéå¨æ¹æ³
- ä¸ä¸ä¸ªå ç´ ï¼
$("#id").next()
$("#id").nextAll()
$("#id").nextUntil("#i2")
- ä¸ä¸ä¸ªå ç´ ï¼
$("#id").prev()
$("#id").prevAll()
$("#id").prevUntil("#i2")
- ç¶äº²å ç´ ï¼
$("#id").parent()
$("#id").parents() // æ¥æ¾å½åå
ç´ çææçç¶è¾å
ç´
$("#id").parentsUntil() // æ¥æ¾å½åå
ç´ çææçç¶è¾å
ç´ ï¼ç´å°éå°å¹é
çé£ä¸ªå
ç´ ä¸ºæ¢ã
- å¿ååå å¼å ç´ ï¼
$("#id").children();// å¿å们
$("#id").siblings();// å
å¼ä»¬
- æ¥æ¾
- æç´¢ææä¸æå®è¡¨è¾¾å¼å¹é çå ç´ ãè¿ä¸ªå½æ°æ¯æ¾åºæ£å¨å¤ççå ç´ çå代å ç´ ç好æ¹æ³ã
$("div").find("p")
çä»·äº$("div p")
- çé
- çéåºä¸æå®è¡¨è¾¾å¼å¹é çå ç´ éåãè¿ä¸ªæ¹æ³ç¨äºç¼©å°å¹é çèå´ãç¨éå·åéå¤ä¸ªè¡¨è¾¾å¼ã
$("div").filter(".c1") // ä»ç»æéä¸è¿æ»¤åºæc1æ ·å¼ç±»ç
çä»·äº
$("div.c1")
è¡¥å ï¼
.first() // è·åå¹é
ç第ä¸ä¸ªå
ç´
.last() // è·åå¹é
çæåä¸ä¸ªå
ç´
.not() // ä»å¹é
å
ç´ çéåä¸å é¤ä¸æå®è¡¨è¾¾å¼å¹é
çå
ç´
.has() // ä¿çå
å«ç¹å®å代çå
ç´ ï¼å»æé£äºä¸å«ææå®å代çå
ç´ ã
.eq() // ç´¢å¼å¼çäºæå®å¼çå
ç´
- 左侧èå示ä¾
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>左侧èå示ä¾</title>
<style>
.left {
position: fixed;
left: 0;
top: 0;
width: 20%;
height: 100%;
background-color: rgb(47, 53, 61);
}
.right {
width: 80%;
height: 100%;
}
.menu {
color: white;
}
.title {
text-align: center;
padding: 10px 15px;
border-bottom: 1px solid #23282e;
}
.items {
background-color: #181c20;
}
.item {
padding: 5px 10px;
border-bottom: 1px solid #23282e;
}
.hide {
display: none;
}
</style>
</head>
<body>
<div class="left">
<div class="menu">
<div class="title">èåä¸</div>
<div class="items">
<div class="item">111</div>
<div class="item">222</div>
<div class="item">333</div>
</div>
<div class="title">èåäº</div>
<div class="items hide">
<div class="item">111</div>
<div class="item">222</div>
<div class="item">333</div>
</div>
<div class="title">èåä¸</div>
<div class="items hide">
<div class="item">111</div>
<div class="item">222</div>
<div class="item">333</div>
</div>
</div>
</div>
<div class="right"></div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
$(".title").click(function (){ // jQueryç»å®äºä»¶
// éèææclasséæ.itemsçæ ç¾
$(".items").addClass("hide"); //æ¹éæä½
$(this).next().removeClass("hide");
});
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <meta charset="UTF-8"> -->
<meta http-equiv="content-type" charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>Title</title>
<style>
.menu {
width: 100px;
border: 1px solid darkgrey;
}
.item-title {
height: 30px;
line-height: 35px;
background-color: #449900;
color: white;
text-align: center;
border-bottom: 1px dotted darkgreen;
}
.hide {
display: none;
}
</style>
</head>
<body>
<div class="menu">
<div class="item">
<div class="item-title">èåä¸</div>
<div class="item-body hide">
<div>å
容1</div>
<div>å
容2</div>
<div>å
容3</div>
</div>
</div>
<div class="item">
<div class="item-title">èåäº</div>
<div class="item-body hide">
<div>å
容1</div>
<div>å
容2</div>
<div>å
容3</div>
</div>
</div>
<div class="item">
<div class="item-title">èåä¸</div>
<div class="item-body hide">
<div>å
容1</div>
<div>å
容2</div>
<div>å
容3</div>
</div>
</div>
</div>
<!--<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
var $titleEles = $(".item-title");
for (var i=0;i<$titleEles.length;i++){
$titleEles[i].onclick=function () {
console.log(this);
// // ææèªå·±çhide移é¤
// // $(this).next().removeClass('hide');
// $(this).next().toggleClass('hide');
// // æå
¶å®ç item-body æ ç¾ æ·»å ä¸ hide
// $(this).parent().siblings().find(".item-body").addClass("hide");
$(this).next().toggleClass('hide').parent().siblings().find(".item-body").addClass("hide");
}
}
// ä¸æ¡ä»£ç ç´æ¥å®æ
// $(".item-title").click(function () {
// $(this).next().toggleClass('hide').parent().siblings().find(".item-body").addClass("hide");
// })
</script>
</body>
</html>
9ï¼ æä½æ ç¾
9.1 æ ·å¼æä½
- æ ·å¼ç±»
addClass();// æ·»å æå®çCSSç±»åã
removeClass();// 移é¤æå®çCSSç±»åã
hasClass();// å¤ææ ·å¼åä¸åå¨
toggleClass();// åæ¢CSSç±»åï¼å¦ææ就移é¤ï¼å¦æ没æ就添å ã
- 示ä¾ï¼å¼å ³ç¯å模ææ¡
- CSS
css("color","red")//DOMæä½ï¼tag.style.color="red"
- 示ä¾ï¼
$("p").css("color", "red"); //å°ææpæ ç¾çåä½è®¾ç½®ä¸ºçº¢è²
9.2 ä½ç½®æä½
offset()// è·åå¹é
å
ç´ å¨å½åçªå£çç¸å¯¹å移æ设置å
ç´ ä½ç½®
position()// è·åå¹é
å
ç´ ç¸å¯¹ç¶å
ç´ çå移
scrollTop()// è·åå¹é
å
ç´ ç¸å¯¹æ»å¨æ¡é¡¶é¨çå移
scrollLeft()// è·åå¹é
å
ç´ ç¸å¯¹æ»å¨æ¡å·¦ä¾§çå移
-
.offset()
æ¹æ³å 许æ们æ£ç´¢ä¸ä¸ªå ç´ ç¸å¯¹äºææ¡£ï¼documentï¼çå½åä½ç½®ã å
.position()
çå·®å«å¨äºï¼.position()
æ¯ç¸å¯¹äºç¸å¯¹äºç¶çº§å ç´ çä½ç§»ãè¿å顶é¨ç¤ºä¾
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ä½ç½®ç¸å
³ç¤ºä¾ä¹è¿å顶é¨</title>
<style>
.c1 {
width: 100px;
height: 200px;
background-color: red;
}
.c2 {
height: 50px;
width: 50px;
position: fixed;
bottom: 15px;
right: 15px;
background-color: #2b669a;
}
.hide {
display: none;
}
.c3 {
height: 100px;
}
</style>
</head>
<body>
<button id="b1" class="btn btn-default">ç¹æ</button>
<div class="c1"></div>
<div class="c3">1</div>
<div class="c3">2</div>
<div class="c3">3</div>
<div class="c3">4</div>
<div class="c3">5</div>
<div class="c3">6</div>
<div class="c3">7</div>
<div class="c3">8</div>
<div class="c3">9</div>
<div class="c3">10</div>
<div class="c3">11</div>
<div class="c3">12</div>
<div class="c3">13</div>
<div class="c3">14</div>
<div class="c3">15</div>
<div class="c3">16</div>
<div class="c3">17</div>
<div class="c3">18</div>
<div class="c3">19</div>
<div class="c3">20</div>
<div class="c3">21</div>
<div class="c3">22</div>
<div class="c3">23</div>
<div class="c3">24</div>
<div class="c3">25</div>
<div class="c3">26</div>
<div class="c3">27</div>
<div class="c3">28</div>
<div class="c3">29</div>
<div class="c3">30</div>
<div class="c3">31</div>
<div class="c3">32</div>
<div class="c3">33</div>
<div class="c3">34</div>
<div class="c3">35</div>
<div class="c3">36</div>
<div class="c3">37</div>
<div class="c3">38</div>
<div class="c3">39</div>
<div class="c3">40</div>
<div class="c3">41</div>
<div class="c3">42</div>
<div class="c3">43</div>
<div class="c3">44</div>
<div class="c3">45</div>
<div class="c3">46</div>
<div class="c3">47</div>
<div class="c3">48</div>
<div class="c3">49</div>
<div class="c3">50</div>
<div class="c3">51</div>
<div class="c3">52</div>
<div class="c3">53</div>
<div class="c3">54</div>
<div class="c3">55</div>
<div class="c3">56</div>
<div class="c3">57</div>
<div class="c3">58</div>
<div class="c3">59</div>
<div class="c3">60</div>
<div class="c3">61</div>
<div class="c3">62</div>
<div class="c3">63</div>
<div class="c3">64</div>
<div class="c3">65</div>
<div class="c3">66</div>
<div class="c3">67</div>
<div class="c3">68</div>
<div class="c3">69</div>
<div class="c3">70</div>
<div class="c3">71</div>
<div class="c3">72</div>
<div class="c3">73</div>
<div class="c3">74</div>
<div class="c3">75</div>
<div class="c3">76</div>
<div class="c3">77</div>
<div class="c3">78</div>
<div class="c3">79</div>
<div class="c3">80</div>
<div class="c3">81</div>
<div class="c3">82</div>
<div class="c3">83</div>
<div class="c3">84</div>
<div class="c3">85</div>
<div class="c3">86</div>
<div class="c3">87</div>
<div class="c3">88</div>
<div class="c3">89</div>
<div class="c3">90</div>
<div class="c3">91</div>
<div class="c3">92</div>
<div class="c3">93</div>
<div class="c3">94</div>
<div class="c3">95</div>
<div class="c3">96</div>
<div class="c3">97</div>
<div class="c3">98</div>
<div class="c3">99</div>
<div class="c3">100</div>
<button id="b2" class="btn btn-default c2 hide">è¿å顶é¨</button>
<script src="jquery-3.2.1.min.js"></script>
<script>
$("#b1").on("click", function () {
$(".c1").offset({left: 200, top:200});
});
$(window).scroll(function () {
if ($(window).scrollTop() > 100) {
$("#b2").removeClass("hide");
}else {
$("#b2").addClass("hide");
}
});
$("#b2").on("click", function () {
$(window).scrollTop(0);
})
</script>
</body>
</html>
9.3 尺寸
height()
width()
innerHeight()
innerWidth()
outerHeight()
outerWidth()
9.4 ææ¬æä½
- HTML代ç ï¼
html()// åå¾ç¬¬ä¸ä¸ªå¹é
å
ç´ çhtmlå
容
html(val)// 设置ææå¹é
å
ç´ çhtmlå
容
- ææ¬å¼ï¼
text()// åå¾ææå¹é
å
ç´ çå
容
text(val)// 设置ææå¹é
å
ç´ çå
容
- å¼ï¼
val()// åå¾ç¬¬ä¸ä¸ªå¹é
å
ç´ çå½åå¼
val(val)// 设置ææå¹é
å
ç´ çå¼
val([val1, val2])// 设置å¤éçcheckboxãå¤éselectçå¼
- ä¾å¦ï¼
<input type="checkbox" value="basketball" name="hobby">篮ç
<input type="checkbox" value="football" name="hobby">足ç
<select multiple id="s1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
- 设置å¼ï¼
$("[name='hobby']").val(['basketball', 'football']);
$("#s1").val(["1", "2"])
- 示ä¾ï¼
- è·å被éä¸çcheckboxæradioçå¼ï¼
<label for="c1">女</label>
<input name="gender" id="c1" type="radio" value="0">
<label for="c2">ç·</label>
<input name="gender" id="c2" type="radio" value="1">
- å¯ä»¥ä½¿ç¨ï¼
$("input[name='gender']:checked").val()
- èªå®ä¹ç»å½æ ¡éªç¤ºä¾
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ææ¬æä½ä¹ç»å½éªè¯</title>
<style>
.error {
color: red;
}
</style>
</head>
<body>
<form action="">
<div>
<label for="input-name">ç¨æ·å</label>
<input type="text" id="input-name" name="name">
<span class="error"></span>
</div>
<div>
<label for="input-password">å¯ç </label>
<input type="password" id="input-password" name="password">
<span class="error"></span>
</div>
<div>
<input type="button" id="btn" value="æ交">
</div>
</form>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
$("#btn").click(function () {
var username = $("#input-name").val();
var password = $("#input-password").val();
if (username.length === 0) {
$("#input-name").siblings(".error").text("ç¨æ·åä¸è½ä¸ºç©º");
}
if (password.length === 0) {
$("#input-password").siblings(".error").text("å¯ç ä¸è½ä¸ºç©º");
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <meta charset="UTF-8"> -->
<meta http-equiv="content-type" charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>ç»å½éªè¯</title>
</head>
<body>
<form action="" id="f1">
<p>
<label>ç¨æ·åï¼
<input type="text" name="username" id="i1">
<span></span>
</label>
</p>
<p>
<label>å¯ç ï¼
<input type="password" name="password" id="i2">
<span></span>
</label>
</p>
<button type="button" id="b1">ç»å½</button>
</form>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#b1").click(function () {
var $inputEles = $("#f1 input");
for (var i=0;i<$inputEles.length;i++){
var tmp = $inputEles[i];
if ($(tmp).val().length === 0){
// 表示该inputæ¡çå¼ä¸ºç©º
console.log($(tmp).parent().text().trim().slice(0,-1));
var s = $(tmp).parent().text().trim().slice(0,-1);
$(tmp).next().text(s + "ä¸è½ä¸ºç©º").css("color", "red");
}
}
});
</script>
</body>
</html>
9.5 å±æ§æä½
- ç¨äºIDçæèªå®ä¹å±æ§ï¼
attr(attrName)// è¿å第ä¸ä¸ªå¹é
å
ç´ çå±æ§å¼
attr(attrName, attrValue)// 为ææå¹é
å
ç´ è®¾ç½®ä¸ä¸ªå±æ§å¼
attr({k1: v1, k2:v2})// 为ææå¹é
å
ç´ è®¾ç½®å¤ä¸ªå±æ§å¼
removeAttr()// ä»æ¯ä¸ä¸ªå¹é
çå
ç´ ä¸å é¤ä¸ä¸ªå±æ§
- ç¨äºcheckboxåradio
prop() // è·åå±æ§
removeProp() // 移é¤å±æ§
- 注æï¼
- å¨1.xå2.xçæ¬çjQueryä¸ä½¿ç¨attr对checkboxè¿è¡èµå¼æä½æ¶ä¼åºbugï¼å¨3.xçæ¬çjQueryä¸å没æè¿ä¸ªé®é¢ã为äºå ¼å®¹æ§ï¼æ们å¨å¤çcheckboxåradioçæ¶åå°½é使ç¨ç¹å®çprop()ï¼ä¸è¦ä½¿ç¨attr("checked", "checked")ã
<input type="checkbox" value="1">
<input type="radio" value="2">
<script>
$(":checkbox[value='1']").prop("checked", true);
$(":radio[value='2']").prop("checked", true);
</script>
- propåattrçåºå«ï¼
- attrå ¨ç§°attribute(å±æ§)
- propå ¨ç§°property(å±æ§)
- è½ç¶é½æ¯å±æ§ï¼ä½ä»ä»¬ææçå±æ§å¹¶ä¸ç¸åï¼attrææçå±æ§æ¯HTMLæ ç¾å±æ§ï¼èpropææçæ¯DOM对象å±æ§ï¼å¯ä»¥è®¤ä¸ºattræ¯æ¾å¼çï¼èpropæ¯éå¼çã
- 举个ä¾åï¼
<input type="checkbox" id="i1" value="1">
- é对ä¸é¢ç代ç ï¼
$("#i1").attr("checked") // undefined
$("#i1").prop("checked") // false
- å¯ä»¥çå°attrè·åä¸ä¸ªæ ç¾å 没æçä¸è¥¿ä¼å¾å°undefinedï¼èpropè·åçæ¯è¿ä¸ªDOM对象çå±æ§ï¼å æ¤checked为falseã
- å¦ææ¢æä¸é¢ç代ç ï¼
<input type="checkbox" checked id="i1" value="1">
- åæ§è¡ï¼
$("#i1").attr("checked") // checked
$("#i1").prop("checked") // true
- è¿å·²ç»å¯ä»¥è¯æattrçå±éæ§ï¼å®çä½ç¨èå´åªéäºHTMLæ ç¾å çå±æ§ï¼èpropè·åçæ¯è¿ä¸ªDOM对象çå±æ§ï¼éä¸è¿åtrueï¼æ²¡éä¸è¿åfalseã
- æ¥ä¸æ¥åçä¸ä¸é对èªå®ä¹å±æ§ï¼attråpropåæä»ä¹åºå«ï¼
<input type="checkbox" checked id="i1" value="1" me="èªå®ä¹å±æ§">
- æ§è¡ä»¥ä¸ä»£ç ï¼
$("#i1").attr("me") // "èªå®ä¹å±æ§"
$("#i1").prop("me") // undefined
å¯ä»¥çå°propä¸æ¯æè·åæ ç¾çèªå®ä¹å±æ§ã
- æ»ç»ä¸ä¸ï¼
- 对äºæ ç¾ä¸æçè½çå°çå±æ§åèªå®ä¹å±æ§é½ç¨attr
- 对äºè¿åå¸å°å¼çæ¯å¦checkboxãradioåoptionçæ¯å¦è¢«éä¸é½ç¨propã
ä¾ï¼å ¨éãåéãåæ¶
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <meta charset="UTF-8"> -->
<meta http-equiv="content-type" charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>Title</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>åºå·</th>
<th>å§å</th>
<th>ç±å¥½</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"></td>
<td>åµåµ</td>
<td>微微ä¸ç¬</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>å»å»</td>
<td>çå
³ç´§å¬</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>åå</td>
<td>å£å¦æ¬æ²³</td>
</tr>
</tbody>
</table>
<hr>
<button id="b1">å
¨é</button>
<button id="b2">åé</button>
<button id="b3">åæ¶</button>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
// å
¨é
$("#b1").click(function () {
// æ¾å°ææçcheckbox,éä¸ï¼prop("che;cker",true)
$(":checkbox").prop("checked", true);
});
// åæ¶
$("#b3").click(function () {
// æ¾å°ææçcheckbox,éä¸ï¼prop("che;cker",false)
$(":checkbox").prop("checked", false);
});
// åé
$("#b2").click(function () {
// æ¹æ³ä¸ï¼
// // æ¾å°ææéä¸çcheckboxåæ¶éä¸
// $("input:checked").prop("checked", false);
// // æ¾å°ææ没éä¸çCheckBox éä¸
// $("input:not(:checked)").prop("checked", true);
// 以ä¸æ³¨é代ç 为æè·¯ï¼ä¸æ£ç¡®
// æ¾å°ææéä¸ç checkbox;
var $checked = $("input:checked");
// æ¾å°æ²¡æéä¸ä¸
var $unchecked = $("input:not(:checked)");
$check.prop("checked", false);
$unchecked.prop("checked", true);
// // æ¹æ³äºï¼
// var $check = $(":checkbox");
// for (var i=0;i<$check.length;i++){
// var tmp = $check[i];
// var s = $(tmp).prop("checked");
// // å¦æsæ¯trueå°±æ¹æfalseï¼å¦ææ¯falseå°±æ¹ætrue
// // if (s){
// // $(tmp).prop("checked", false);
// // }else{
// // $(tmp).prop("checked", true);
// // }
// // ç®å
// $(tmp).prop("checked", !s);
// }
// // æ¹æ³ä¸:
// $(":checkbox").each(function () {
// var value = $(this).prop("checked");
// $(this).prop("checked", !value)
// });
});
</script>
</body>
</html>
9.6 ææ¡£å¤ç
- æ·»å å°æå®å ç´ å é¨çåé¢
$(A).append(B)// æB追å å°A
$(A).appendTo(B)// æA追å å°B
- æ·»å å°æå®å ç´ å é¨çåé¢
$(A).prepend(B)// æBåç½®å°A
$(A).prependTo(B)// æAåç½®å°B
- æ·»å å°æå®å ç´ å¤é¨çåé¢
$(A).after(B)// æBæ¾å°Açåé¢
$(A).insertAfter(B)// æAæ¾å°Bçåé¢
- æ·»å å°æå®å ç´ å¤é¨çåé¢
$(A).before(B)// æBæ¾å°Açåé¢
$(A).insertBefore(B)// æAæ¾å°Bçåé¢
- 移é¤åæ¸ ç©ºå ç´
remove()// ä»DOMä¸å é¤ææå¹é
çå
ç´ ã
empty()// å é¤å¹é
çå
ç´ éåä¸ææçåèç¹ã
- ä¾åï¼
- ç¹å»æé®å¨è¡¨æ ¼æ·»å ä¸è¡æ°æ®ã
- ç¹å»æ¯ä¸è¡çå é¤æé®å é¤å½åè¡æ°æ®ã
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <meta charset="UTF-8"> -->
<meta http-equiv="content-type" charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>Title</title>
</head>
<body>
<button id="b1">æ·»å </button>
<table border="1">
<thead>
<tr>
<th>åºå·</th>
<th>å§å</th>
<th>ç±å¥½</th>
<th>æä½</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>åµåµ</td>
<td>微微ä¸ç¬</td>
<td><button class="delete">å¼é¤</button></td>
</tr>
<tr>
<td>2</td>
<td>å»å»</td>
<td>é¾çå§å´</td>
<td><button class="delete">å¼é¤</button></td>
</tr>
</tbody>
</table>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#b1").click(function () {
// å¨è¡¨æ ¼çæåæ·»å ä¸è¡æ°æ®
// 1, å
ææ°æ®
var trEle = document.createElement("tr"); //trEle æ¯ä¸ä¸ª DOM 对象
// var tdEle1 = document.createElement("td");
// tdEle1.innerText = "3";
// $(trEle).append(tdEle1);
// var tdEle2 = document.createElement("td");
// tdEle2.innerText = "åå";
// $(trEle).append(tdEle2);
// var tdEle3 = document.createElement("td");
// tdEle3.innerText = "å£å¦æ¬æ²³";
// $(trEle).append(tdEle3);
trEle.innerHTML = `
<td>3</td>
<td>åå</td>
<td>å£å¦æ¬æ²³</td>
<td><button class="delete">å¼é¤</button></td>
`;
// 2 追å å°tbodyçæå
$("tbody").append(trEle);
});
// // æ¾å¼é¤æé®ï¼ç»å®äºä»¶
// $(".delete").click(function () {
// // this æçå°±æ¯è°è§¦åçäºä»¶ï¼thisæ¯ä¸ä¸ªDOM对象
// console.log(this);
// $(this).parent().parent().remove();
// // $(this).parentsUntil("tbody").remove();
// });
// 使ç¨äºä»¶å§æçæ¹å¼ç»æªæ¥çæ ç¾ç»å®äºä»¶
$("table").on("click", ".delete", function () {
console.log(this);
// this æçå°±æ¯è°è§¦åçäºä»¶ï¼thisæ¯ä¸ä¸ªDOM对象
$(this).parent().parent().remove();
});
</script>
</body>
</html>
- æ¿æ¢
replaceWith()
replaceAll()
- å é
clone()// åæ°
- å é示ä¾ï¼
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>å
é</title>
<style>
#b1 {
background-color: deeppink;
padding: 5px;
color: white;
margin: 5px;
}
#b2 {
background-color: dodgerblue;
padding: 5px;
color: white;
margin: 5px;
}
</style>
</head>
<body>
<button id="b1">å± é¾å®åï¼ç¹å»å°±é</button>
<hr>
<button id="b2">å± é¾å®åï¼ç¹å»å°±é</button>
<script src="jquery-3.2.1.min.js"></script>
<script>
// cloneæ¹æ³ä¸å åæ°trueï¼å
éæ ç¾ä½ä¸å
éæ ç¾å¸¦çäºä»¶
$("#b1").on("click", function () {
$(this).clone().insertAfter(this);
});
// cloneæ¹æ³å åæ°trueï¼å
éæ ç¾å¹¶ä¸å
éæ ç¾å¸¦çäºä»¶
$("#b2").on("click", function () {
$(this).clone(true).insertAfter(this);
});
</script>
</body>
</html>
10ï¼ äºä»¶
10.1 常ç¨äºä»¶
click(function(){...})
hover(function(){...})
blur(function(){...})
focus(function(){...})
change(function(){...})
keyup(function(){...})
- keydownåkeyupäºä»¶ç»å示ä¾ï¼
- æä½shiftå®ç°æ¹éæä½
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="content-Type" charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>Title</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>#</th>
<th>å§å</th>
<th>æä½</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"></td>
<td>Egon</td>
<td>
<select>
<option value="1">ä¸çº¿</option>
<option value="2">ä¸çº¿</option>
<option value="3">åè</option>
</select>
</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>Alex</td>
<td>
<select>
<option value="1">ä¸çº¿</option>
<option value="2">ä¸çº¿</option>
<option value="3">åè</option>
</select>
</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>Yuan</td>
<td>
<select>
<option value="1">ä¸çº¿</option>
<option value="2">ä¸çº¿</option>
<option value="3">åè</option>
</select>
</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>EvaJ</td>
<td>
<select>
<option value="1">ä¸çº¿</option>
<option value="2">ä¸çº¿</option>
<option value="3">åè</option>
</select>
</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>Gold</td>
<td>
<select>
<option value="1">ä¸çº¿</option>
<option value="2">ä¸çº¿</option>
<option value="3">åè</option>
</select>
</td>
</tr>
</tbody>
</table>
<input type="button" id="b1" value="å
¨é">
<input type="button" id="b2" value="åæ¶">
<input type="button" id="b3" value="åé">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
var flag = false;
// shiftæé®è¢«æä¸çæ¶å
$(window).keydown(function (event) {
console.log(event.keyCode);
if (event.keyCode === 16){
flag = true;
}
});
// shiftæé®è¢«æ¬èµ·çæ¶å
$(window).keyup(function (event) {
console.log(event.keyCode);
if (event.keyCode === 16){
flag = false;
}
});
// selectæ ç¾çå¼åçååçæ¶å
$("select").change(function (event) {
// å¦æshiftæé®è¢«æä¸ï¼å°±è¿å
¥æ¹éç¼è¾æ¨¡å¼
// shiftæé®å¯¹åºçcodeæ¯16
// å¤æå½åselectè¿ä¸è¡æ¯å¦è¢«éä¸
console.log($(this).parent().siblings().first().find(":checkbox"));
var isChecked = $(this).parent().siblings().first().find(":checkbox").prop("checked");
console.log(isChecked);
if (flag && isChecked) {
// è¿å
¥æ¹éç¼è¾æ¨¡å¼
// 1. åå°å½åselectéä¸çå¼
var value = $(this).val();
// 2. ç»å
¶ä»è¢«éä¸è¡çselect设置æåæä¸æ ·çå¼
// 2.1 æ¾å°é£äºè¢«éä¸è¡çselect
var $select = $("input:checked").parent().parent().find("select")
// 2.2 ç»éä¸çselectèµå¼
$select.val(value);
}
});
</script>
</body>
</html>
- hoveräºä»¶ç¤ºä¾ï¼
- hoveräºä»¶
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>hover示ä¾</title>
<style>
* {
margin: 0;
padding: 0;
}
.nav {
height: 40px;
width: 100%;
background-color: #3d3d3d;
position: fixed;
top: 0;
}
.nav ul {
list-style-type: none;
line-height: 40px;
}
.nav li {
float: left;
padding: 0 10px;
color: #999999;
position: relative;
}
.nav li:hover {
background-color: #0f0f0f;
color: white;
}
.clearfix:after {
content: "";
display: block;
clear: both;
}
.son {
position: absolute;
top: 40px;
right: 0;
height: 50px;
width: 100px;
background-color: #00a9ff;
display: none;
}
.hover .son {
display: block;
}
</style>
</head>
<body>
<div class="nav">
<ul class="clearfix">
<li>ç»å½</li>
<li>注å</li>
<li>è´ç©è½¦
<p class="son hide">
空空å¦ä¹...
</p>
</li>
</ul>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$(".nav li").hover(
function () {
$(this).addClass("hover");
},
function () {
$(this).removeClass("hover");
}
);
</script>
</body>
</html>
- å®æ¶çå¬inputè¾å
¥å¼åå示ä¾ï¼
- inputå¼ååäºä»¶
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>å®æ¶çå¬inputè¾å
¥å¼åå</title>
</head>
<body>
<input type="text" id="i1">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
/*
* oninputæ¯HTML5çæ åäºä»¶
* è½å¤æ£æµtextarea,input:text,input:passwordåinput:searchè¿å 个å
ç´ çå
容ååï¼
* å¨å
容修æ¹åç«å³è¢«è§¦åï¼ä¸åonchangeäºä»¶éè¦å¤±å»ç¦ç¹æ触å
* oninputäºä»¶å¨IE9以ä¸çæ¬ä¸æ¯æï¼éè¦ä½¿ç¨IEç¹æçonpropertychangeäºä»¶æ¿ä»£
* 使ç¨jQueryåºçè¯ç´æ¥ä½¿ç¨onåæ¶ç»å®è¿ä¸¤ä¸ªäºä»¶å³å¯ã
* */
$("#i1").on("input propertychange", function () {
alert($(this).val());
})
</script>
</body>
</html>
10.2 äºä»¶ç»å®
-
.on( events [, selector ],function(){})
-
events
ï¼ äºä»¶ -
selector
: éæ©å¨ï¼å¯éçï¼ -
function
: äºä»¶å¤çå½æ°
-
10.3 移é¤äºä»¶
.off( events [, selector ][,function(){}])
-
off()
æ¹æ³ç§»é¤ç¨.on()
ç»å®çäºä»¶å¤çç¨åºã-
events
ï¼ äºä»¶ -
selector
: éæ©å¨ï¼å¯éçï¼ -
function
: äºä»¶å¤çå½æ°
-
10.4 é»æ¢åç»äºä»¶æ§è¡
- 1
return false; // 常è§é»æ¢è¡¨åæ交ç
- 2
e.preventDefault();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>é»æ¢é»è®¤äºä»¶</title>
</head>
<body>
<form action="">
<button id="b1">ç¹æ</button>
</form>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#b1").click(function (e) {
alert(123);
//return false;
e.preventDefault();
});
</script>
</body>
</html>
- 注æï¼
- åclickãkeydownçDOMä¸å®ä¹çäºä»¶ï¼æ们é½å¯ä»¥ä½¿ç¨
.on()
æ¹æ³æ¥ç»å®äºä»¶ï¼ä½æ¯hover
è¿ç§jQueryä¸å®ä¹çäºä»¶å°±ä¸è½ç¨.on()
æ¹æ³æ¥ç»å®äºã - æ³ä½¿ç¨äºä»¶å§æçæ¹å¼ç»å®hoveräºä»¶å¤çå½æ°ï¼å¯ä»¥åç §å¦ä¸ä»£ç å两æ¥ç»å®äºä»¶ï¼
- åclickãkeydownçDOMä¸å®ä¹çäºä»¶ï¼æ们é½å¯ä»¥ä½¿ç¨
$('ul').on('mouseenter', 'li', function() {//ç»å®é¼ æ è¿å
¥äºä»¶
$(this).addClass('hover');
});
$('ul').on('mouseleave', 'li', function() {//ç»å®é¼ æ ååºäºä»¶
$(this).removeClass('hover');
});
10.5 é»æ¢äºä»¶å泡
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <meta charset="UTF-8"> -->
<meta http-equiv="content-type" charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>é»æ¢äºä»¶å泡</title>
</head>
<body>
<div id="d1">
<p id="p1">
<sapn id="s1">span</sapn>
</p>
</div>
<hr>
<form action="">
<input type="text" id="i2">
<input type="submit" value="æ交" id="i1">
</form>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#s1").click(function (event) {
// event 表示äºä»¶æ¬èº«
alert("è¿æ¯span");
// é»æ¢äºä»¶å泡
event.stopPropagation()
});
$("#p1").click(function () {
alert("è¿æ¯pæ ç¾");
});
$("#d1").click(function () {
alert("è¿æ¯divæ ç¾");
});
// form 表å
// ç¹å»submitæé®ï¼å
æ ¡éªinputæ¡çå¼ä¸ºä¸ä¸ºç©º
// ä¸ä¸ºç©ºå°±æ交
$("#i1").click(function (event) {
alert("è¿æ¯form表åæ交æé®");
var value = $("#i2").val();
if (value.length === 0){
// 为空ä¸æ交
// ä¸æ§è¡åç»é»è®¤çæ交äºä»¶
// é»æ¢é»è®¤äºä»¶çæ§è¡(é常ç¨äºé»æ¢form表åçæ交)
event.preventDefault();
}
});
</script>
</body>
</html>
10.6 页é¢è½½å ¥
- å½DOMè½½å ¥å°±ç»ªå¯ä»¥æ¥è¯¢åæ纵æ¶ç»å®ä¸ä¸ªè¦æ§è¡çå½æ°ãè¿æ¯äºä»¶æ¨¡åä¸æéè¦çä¸ä¸ªå½æ°ï¼å 为å®å¯ä»¥æ大å°æé«webåºç¨ç¨åºçååºé度ã
- 两ç§åæ³ï¼
$(document).ready(function(){
// å¨è¿éåä½ çJS代ç ...
})
- ç®åï¼
$(function(){
// ä½ å¨è¿éåä½ ç代ç
})
- ææ¡£å è½½å®ç»å®äºä»¶ï¼å¹¶ä¸é»æ¢é»è®¤äºä»¶åçï¼
- ç»å½æ ¡éªç¤ºä¾
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ç»å½æ³¨å示ä¾</title>
<style>
.error {
color: red;
}
</style>
</head>
<body>
<form id="myForm">
<label for="name">å§å</label>
<input type="text" id="name">
<span class="error"></span>
<label for="passwd">å¯ç </label>
<input type="password" id="passwd">
<span class="error"></span>
<input type="submit" id="modal-submit" value="ç»å½">
</form>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="s7validate.js"></script>
<script>
function myValidation() {
// å¤æ¬¡ç¨å°çjQuery对象åå¨å°ä¸ä¸ªåéï¼é¿å
éå¤æ¥è¯¢ææ¡£æ
var $myForm = $("#myForm");
$myForm.find(":submit").on("click", function () {
// å®ä¹ä¸ä¸ªæ å¿ä½ï¼è®°å½è¡¨åå¡«åæ¯å¦æ£å¸¸
var flag = true;
$myForm.find(":text, :password").each(function () {
var val = $(this).val();
if (val.length <= 0 ){
var labelName = $(this).prev("label").text();
$(this).next("span").text(labelName + "ä¸è½ä¸ºç©º");
flag = false;
}
});
// 表åå¡«åæ误就ä¼è¿åfalseï¼é»æ¢submitæé®é»è®¤çæ交表åäºä»¶
return flag;
});
// inputè¾å
¥æ¡è·åç¦ç¹å移é¤ä¹åçé误æ示信æ¯
$myForm.find("input[type!='submit']").on("focus", function () {
$(this).next(".error").text("");
})
}
// ææ¡£æ 就绪åæ§è¡
$(document).ready(function () {
myValidation();
});
</script>
</body>
</html>
10.7 ä¸window.onloadçåºå«
-
window.onload()
å½æ°æè¦çç°è±¡ï¼å¿ é¡»çå¾ çå¾çèµæºå è½½å®æä¹åæè½è°ç¨ - jQueryçè¿ä¸ªå ¥å£å½æ°æ²¡æå½æ°è¦çç°è±¡ï¼ææ¡£å è½½å®æä¹åå°±å¯ä»¥è°ç¨ï¼å»ºè®®ä½¿ç¨æ¤å½æ°ï¼
10.8 äºä»¶å§æ
- äºä»¶å§ææ¯éè¿äºä»¶å泡çåçï¼å©ç¨ç¶æ ç¾å»æè·åæ ç¾çäºä»¶ã
- 示ä¾ï¼
- è¡¨æ ¼ä¸æ¯ä¸è¡çç¼è¾åå é¤æé®é½è½è§¦åç¸åºçäºä»¶ã
$("table").on("click", ".delete", function () {
// å é¤æé®ç»å®çäºä»¶
})
11ï¼ å¨ç»ææ
// åºæ¬
show([s,[e],[fn]])
hide([s,[e],[fn]])
toggle([s],[e],[fn])
// æ»å¨
slideDown([s],[e],[fn])
slideUp([s,[e],[fn]])
slideToggle([s],[e],[fn])
// æ·¡å
¥æ·¡åº
fadeIn([s],[e],[fn])
fadeOut([s],[e],[fn])
fadeTo([[s],o,[e],[fn]])
fadeToggle([s,[e],[fn]])
// èªå®ä¹ï¼äºè§£å³å¯ï¼
animate(p,[s],[e],[fn])
- èªå®ä¹å¨ç»ç¤ºä¾ï¼
- ç¹èµç¹æç®å示ä¾
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ç¹èµå¨ç»ç¤ºä¾</title>
<style>
div {
position: relative;
display: inline-block;
}
div>i {
display: inline-block;
color: red;
position: absolute;
right: -16px;
top: -5px;
opacity: 1;
}
</style>
</head>
<body>
<div id="d1">ç¹èµ</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#d1").on("click", function () {
var newI = document.createElement("i");
newI.innerText = "+1";
$(this).append(newI);
$(this).children("i").animate({
opacity: 0
}, 1000)
})
</script>
</body>
</html>
12ï¼ è¡¥å
12.1 each
jQuery.each(collection, callback(indexInArray, valueOfElement))ï¼
- æè¿°ï¼ä¸ä¸ªéç¨çè¿ä»£å½æ°ï¼å®å¯ä»¥ç¨æ¥æ ç¼è¿ä»£å¯¹è±¡åæ°ç»ãæ°ç»å类似æ°ç»ç对象éè¿ä¸ä¸ªé¿åº¦å±æ§ï¼å¦ä¸ä¸ªå½æ°çåæ°å¯¹è±¡ï¼æ¥è¿ä»£æ°åç´¢å¼ï¼ä»0å°length - 1ãå ¶ä»å¯¹è±¡éè¿å ¶å±æ§åè¿è¡è¿ä»£ã
li =[10,20,30,40]
$.each(li,function(i, v){
console.log(i, v);//indexæ¯ç´¢å¼ï¼eleæ¯æ¯æ¬¡å¾ªç¯çå
·ä½å
ç´ ã
})
- è¾åºï¼
010
120
230
340
.each(function(index, Element))ï¼
- æè¿°ï¼éåä¸ä¸ªjQuery对象ï¼ä¸ºæ¯ä¸ªå¹é å ç´ æ§è¡ä¸ä¸ªå½æ°ã
-
.each()
æ¹æ³ç¨æ¥è¿ä»£jQuery对象ä¸çæ¯ä¸ä¸ªDOMå ç´ ãæ¯æ¬¡åè°å½æ°æ§è¡æ¶ï¼ä¼ä¼ éå½å循ç¯æ¬¡æ°ä½ä¸ºåæ°(ä»0å¼å§è®¡æ°)ãç±äºåè°å½æ°æ¯å¨å½åDOMå ç´ ä¸ºä¸ä¸æçè¯å¢ä¸è§¦åçï¼æä»¥å ³é®å this æ»æ¯æåè¿ä¸ªå ç´ ã
// 为æ¯ä¸ä¸ªliæ ç¾æ·»å foo
$("li").each(function(){
$(this).addClass("c1");
});
- 注æ: jQueryçæ¹æ³è¿åä¸ä¸ªjQuery对象ï¼éåjQueryéåä¸çå ç´ - 被称为éå¼è¿ä»£çè¿ç¨ãå½è¿ç§æ åµåçæ¶ï¼å®é常ä¸éè¦æ¾å¼å°å¾ªç¯ç .each()æ¹æ³ï¼
- ä¹å°±æ¯è¯´ï¼ä¸é¢çä¾å没æå¿ è¦ä½¿ç¨each()æ¹æ³ï¼ç´æ¥åä¸é¢è¿æ ·åå°±å¯ä»¥äºï¼
$("li").addClass("c1"); // 对æææ ç¾åç»ä¸æä½
- 注æï¼
å¨éåè¿ç¨ä¸å¯ä»¥ä½¿ç¨ return falseæåç»æeach循ç¯ã
ç»æ¢each循ç¯
return falseï¼
var a1 = [11, 22, 33, 44];
// è·³åºæ¬æ¬¡å¾ªç¯
$.each(a1,function(k,v){
if (v === 33){
return;
}else{
console.log(v);
}
})
# ç»æ
11
22
44
// ç»ææ¬æ¬¡å¾ªç¯
$.each(a1,function(k,v){
if (v === 33){
return false;
}else{
console.log(v);
}
})
# ç»æ
11
22
12.2 .data()
å¨å¹é çå ç´ éåä¸çææå ç´ ä¸åå¨ä»»æç¸å ³æ°æ®æè¿åå¹é çå ç´ éåä¸ç第ä¸ä¸ªå ç´ çç»å®å称çæ°æ®åå¨çå¼ã
.data(key, value):
æè¿°ï¼å¨å¹é çå ç´ ä¸åå¨ä»»æç¸å ³æ°æ®ã
$("div").data("k",100);//ç»æædivæ ç¾é½ä¿åä¸ä¸ªå为kï¼å¼ä¸º100
.data(key):
æè¿°: è¿åå¹é çå ç´ éåä¸ç第ä¸ä¸ªå ç´ çç»å®å称çæ°æ®åå¨çå¼âéè¿
.data(name, value)
æHTML5 data-*
å±æ§è®¾ç½®ã
$("div").data("k");//è¿å第ä¸ä¸ªdivæ ç¾ä¸ä¿åç"k"çå¼
.removeData(key):
- æè¿°ï¼ç§»é¤åæ¾å¨å ç´ ä¸çæ°æ®ï¼ä¸å keyåæ°è¡¨ç¤ºç§»é¤ææä¿åçæ°æ®ã
$("div").removeData("k"); //移é¤å
ç´ ä¸åæ¾k对åºçæ°æ®
- 模ææ¡ç¼è¾çæ°æ®åå¡«è¡¨æ ¼-å¢ãå ãæ¹ãæ¥
- å ¨å±åéç flag
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <meta charset="UTF-8"> -->
<meta http-equiv="content-type" charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>å¢å æ¹æ¥</title>
<style>
.cover {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: rgba(0,0,0,0.4);
z-index: 99;
}
.modal {
position: absolute;
top: 50%;
left: 50%;
height: 200px;
width: 300px;
margin-top: -100px;
margin-left: -250px;
background-color: white;
z-index: 100;
}
.c1 {
margin: 40px 0 0 30px;
}
.hide {
display: none;
}
</style>
<script>
// åçDOMåæ³
window.onload = function () {
// 页é¢å è½½å®ä¹ååçäº
};
// jQueryåæ³
$(document).ready(function () {
// 页é¢å è½½å®ä¹ååçäº
});
</script>
</head>
<body>
<button id="add">æ°å¢</button>
<table border="1">
<thead>
<tr>
<th>#</th>
<th>å§å</th>
<th>ç±å¥½</th>
<th>æä½</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>åµåµ</td>
<td>微微ä¸ç¬</td>
<td>
<button class="edit">ç¼è¾</button>
<button class="delete">å é¤</button>
</td>
</tr>
<tr>
<td>2</td>
<td>å»å»</td>
<td>é¾çå§å´</td>
<td>
<button class="edit">ç¼è¾</button>
<button class="delete">å é¤</button>
</td>
</tr>
<tr>
<td>3</td>
<td>åå</td>
<td>å£å¦æ¬æ²³</td>
<td>
<button class="edit">ç¼è¾</button>
<button class="delete">å é¤</button>
</td>
</tr>
</tbody>
</table>
<!--è¦çå±-->
<div class="cover hide"></div>
<!--模ææ¡-->
<div class="modal hide">
<div class="c1">
<p>
<label for="name">å§åï¼</label>
<input type="text" id="name">
</p>
<p>
<label for="hobby">ç±å¥½ï¼</label>
<input type="text" id="hobby">
</p>
<p>
<input type="button" id="yes" value="æ交">
<input type="button" id="no" value="åæ¶">
</p>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
// (å½æ°å®ä¹)ï¼åæ°ï¼;
// (function () {})($);
(function () {
var flag = null;
// æ¾ç¤ºæ¨¡ææ¡çå½æ°
function showModal() {
$(".cover, .modal").removeClass("hide");
}
// éè模ææ¡çå½æ°
function hideModal() {
// éè模ææ¡ä¹å æ¸
空input
$("#name,#hobby").val("");
$(".cover, .modal").addClass("hide");
}
// ç»æ°å¢æé®ç»å®äºä»¶
$("#add").click(function () {
showModal();
});
// ç»æ¨¡ææ¡ä¸çåæ¶æé®ç»å®äºä»¶
$("#no").on("click", function () {
hideModal();
});
// ç»å é¤æé®ç»å®äºä»¶
$("table").on("click", ".delete", function () {
// ä¿åå½åè¡å°ä¸ä¸ªåé
var $currentTr = $(this).parent().parent();
// 1 æ´æ°åºå·
console.log($currentTr.nextAll());
$currentTr.nextAll().each(function () {
console.log(this);
console.log($(this).children().first().text());
var currentNum = $(this).children().first().text();
$(this).children().first().text(+currentNum-1);
});
// 2 å é¤å½åè¡
console.log($(this).parent().parent());
$currentTr.remove();
});
// ç»æ¨¡ææ¡çæ交æé®ç»å®äºä»¶
$("#yes").click(function () {
// 1 åå°ç¨æ·å¡«åçå¼
var name = $("#name").val();
var hobby = $("#hobby").val();
// æ ¹æ®flagçå¼å¤æä¸ä¸ï¼æ¤æ¬¡æä½æ¶æ¯æ°å¢è¿æ¯æ¿æ¢
if (flag){
// æå¼ç¼è¾
flag.children().eq(1).text(name);
flag.children().eq(2).text(hobby);
// ç¼è¾å®ä¹åæ¸
空flag
flag = null;
}else {
// 没å¼æ°å¢
// 计ç®æ°trçåºå·
var num = $("body tr").length;
// 2 å建træ ç¾
var trEle = `
<tr>
<td>${num}</td>
<td>${name}</td>
<td>${hobby}</td>
<td>
<button class="edit">ç¼è¾</button>
<button class="delete">å é¤</button>
</td>
</tr>
`;
// 3 æå建çtræ ç¾è¿½å å°tbodyä¸
$("tbody").append(trEle);
}
// 4 éè模ææ¡
hideModal();
});
// ç»ç¼è¾æé®ç»å®äºä»¶
$("table").on("click", ".edit", function () {
// 1, å¼¹åºæ¨¡ææ¡
showModal();
// 2ï¼è·åå½åè¡çå¼
var name = $(this).parent().prev().prev().text();
var hobby = $(this).parent().prev().text();
console.log(name, hobby);
// 3ï¼å¡«å
å°æ¨¡ææ¡çinputæ ç¾
$("#name").val(name);
$("#hobby").val(hobby);
flag = $(this).parent().parent();
});
})($);
</script>
</body>
</html>
- dataç
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <meta charset="UTF-8"> -->
<meta http-equiv="content-type" charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>å¢å æ¹æ¥</title>
<style>
.cover {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: rgba(0,0,0,0.4);
z-index: 99;
}
.modal {
position: absolute;
top: 50%;
left: 50%;
height: 200px;
width: 300px;
margin-top: -100px;
margin-left: -250px;
background-color: white;
z-index: 100;
}
.c1 {
margin: 40px 0 0 30px;
}
.hide {
display: none;
}
</style>
<script>
// åçDOMåæ³
window.onload = function () {
// 页é¢å è½½å®ä¹ååçäº
};
// jQueryåæ³
$(document).ready(function () {
// 页é¢å è½½å®ä¹ååçäº
});
</script>
</head>
<body>
<button id="add">æ°å¢</button>
<table border="1">
<thead>
<tr>
<th>#</th>
<th>å§å</th>
<th>ç±å¥½</th>
<th>æä½</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>åµåµ</td>
<td>微微ä¸ç¬</td>
<td>
<button class="edit">ç¼è¾</button>
<button class="delete">å é¤</button>
</td>
</tr>
<tr>
<td>2</td>
<td>å»å»</td>
<td>é¾çå§å´</td>
<td>
<button class="edit">ç¼è¾</button>
<button class="delete">å é¤</button>
</td>
</tr>
<tr>
<td>3</td>
<td>åå</td>
<td>å£å¦æ¬æ²³</td>
<td>
<button class="edit">ç¼è¾</button>
<button class="delete">å é¤</button>
</td>
</tr>
</tbody>
</table>
<!--è¦çå±-->
<div class="cover hide"></div>
<!--模ææ¡-->
<div class="modal hide">
<div class="c1">
<p>
<label for="name">å§åï¼</label>
<input type="text" id="name">
</p>
<p>
<label for="hobby">ç±å¥½ï¼</label>
<input type="text" id="hobby">
</p>
<p>
<input type="button" id="yes" value="æ交">
<input type="button" id="no" value="åæ¶">
</p>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
// (å½æ°å®ä¹)ï¼åæ°ï¼;
// (function () {})($);
(function () {
// æ¾ç¤ºæ¨¡ææ¡çå½æ°
function showModal() {
$(".cover, .modal").removeClass("hide");
}
// éè模ææ¡çå½æ°
function hideModal() {
// éè模ææ¡ä¹å æ¸
空input
$("#name,#hobby").val("");
$(".cover, .modal").addClass("hide");
}
// ç»æ°å¢æé®ç»å®äºä»¶
$("#add").click(function () {
showModal();
});
// ç»æ¨¡ææ¡ä¸çåæ¶æé®ç»å®äºä»¶
$("#no").on("click", function () {
hideModal();
});
// ç»å é¤æé®ç»å®äºä»¶
$("table").on("click", ".delete", function () {
// ä¿åå½åè¡å°ä¸ä¸ªåé
var $currentTr = $(this).parent().parent();
// 1 æ´æ°åºå·
console.log($currentTr.nextAll());
$currentTr.nextAll().each(function () {
console.log(this);
console.log($(this).children().first().text());
var currentNum = $(this).children().first().text();
$(this).children().first().text(+currentNum-1);
});
// 2 å é¤å½åè¡
console.log($(this).parent().parent());
$currentTr.remove();
});
// ç»æ¨¡ææ¡çæ交æé®ç»å®äºä»¶
$("#yes").click(function () {
// 1 åå°ç¨æ·å¡«åçå¼
var name = $("#name").val();
var hobby = $("#hobby").val();
// æ ¹æ®yesæé®ä¸æ没ænumçdataå±æ§
var $num = $("#yes").data("num");
if ($num){
// æå¼ç¼è¾
$num.children().eq(1).text(name);
$num.children().eq(2).text(hobby);
// ç¼è¾å®ä¹åæ¸
空numæ°æ®å±æ§
$("#yes").removeData("num");
}else {
// 没å¼æ°å¢
// 计ç®æ°trçåºå·
var num = $("body tr").length;
// 2 å建træ ç¾
var trEle = `
<tr>
<td>${num}</td>
<td>${name}</td>
<td>${hobby}</td>
<td>
<button class="edit">ç¼è¾</button>
<button class="delete">å é¤</button>
</td>
</tr>
`;
// 3 æå建çtræ ç¾è¿½å å°tbodyä¸
$("tbody").append(trEle);
}
// 4 éè模ææ¡
hideModal();
});
// ç»ç¼è¾æé®ç»å®äºä»¶
$("table").on("click", ".edit", function () {
// 1, å¼¹åºæ¨¡ææ¡
showModal();
// 2ï¼è·åå½åè¡çå¼
var name = $(this).parent().prev().prev().text();
var hobby = $(this).parent().prev().text();
console.log(name, hobby);
// 3ï¼å¡«å
å°æ¨¡ææ¡çinputæ ç¾
$("#name").val(name);
$("#hobby").val(hobby);
$("#yes").data("num", $(this).parent().parent())
});
})($);
</script>
</body>
</html>
12.3 å å 载页é¢åæ§è¡
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <meta charset="UTF-8"> -->
<meta http-equiv="content-type" charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>Title</title>
<script>
// åçDOMåæ³
window.onload = function () {
// 页é¢å è½½å®ä¹ååæ§è¡
};
// jQueryåæ³
$(document).ready(function () {
// 页é¢å è½½å®ä¹ååæ§è¡
});
</script>
</head>
<body>
</body>
</html>
12.4 æ件(äºè§£å³å¯)
jQuery.extend(object)
- jQueryçå½å空é´ä¸æ·»å æ°çåè½ãå¤ç¨äºæ件å¼åè å jQuery ä¸æ·»å æ°å½æ°æ¶ä½¿ç¨ã
- 示ä¾ï¼
<script>
jQuery.extend({
min:function(a, b){return a < b ? a : b;},
max:function(a, b){return a > b ? a : b;}
});
jQuery.min(2,3);// => 2
jQuery.max(4,5);// => 5
</script>
jQuery.fn.extend(object)
- ä¸ä¸ªå¯¹è±¡çå 容å并å°jQueryçååï¼ä»¥æä¾æ°çjQueryå®ä¾æ¹æ³ã
<script>
jQuery.fn.extend({
check:function(){
return this.each(function(){this.checked =true;});
},
uncheck:function(){
return this.each(function(){this.checked =false;});
}
});
// jQuery对象å¯ä»¥ä½¿ç¨æ°æ·»å çcheck()æ¹æ³äºã
$("input[type='checkbox']").check();
</script>
- åç¬åå¨æ件ä¸çæ©å±ï¼
(function(jq){
jq.extend({
funcName:function(){
...
},
});
})(jQuery);
- ä¾åï¼
- èªå®ä¹çjQueryç»å½éªè¯æ件
- HTMLæ件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ç»å½æ ¡éªç¤ºä¾</title>
<style>
.login-form {
margin: 100px auto 0;
max-width: 330px;
}
.login-form > div {
margin: 15px 0;
}
.error {
color: red;
}
</style>
</head>
<body>
<div>
<form action="" class="login-form" novalidate>
<div>
<label for="username">å§å</label>
<input id="username" type="text" name="name" required autocomplete="off">
<span class="error"></span>
</div>
<div>
<label for="passwd">å¯ç </label>
<input id="passwd" type="password" name="password" required autocomplete="off">
<span class="error"></span>
</div>
<div>
<label for="mobile">ææº</label>
<input id="mobile" type="text" name="mobile" required autocomplete="off">
<span class="error"></span>
</div>
<div>
<label for="where">æ¥èª</label>
<input id="where" type="text" name="where" autocomplete="off">
<span class="error"></span>
</div>
<div>
<input type="submit" value="ç»å½">
</div>
</form>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="validate.js"></script>
<script>
$.validate();
</script>
</body>
</html>
- JSæ件
"use strict";
(function ($) {
function check() {
// å®ä¹ä¸ä¸ªæ å¿ä½ï¼è¡¨ç¤ºéªè¯éè¿è¿æ¯éªè¯ä¸éè¿
var flag = true;
var errMsg;
// æ ¡éªè§å
$("form input[type!=':submit']").each(function () {
var labelName = $(this).prev().text();
var inputName = $(this).attr("name");
var inputValue = $(this).val();
if ($(this).attr("required")) {
// å¦ææ¯å¿
填项
if (inputValue.length === 0) {
// å¼ä¸ºç©º
errMsg = labelName + "ä¸è½ä¸ºç©º";
$(this).next().text(errMsg);
flag = false;
return false;
}
// å¦ææ¯å¯ç ç±»åï¼æ们就è¦å¤æå¯ç çé¿åº¦æ¯å¦å¤§äº6ä½
if (inputName === "password") {
// é¤äºä¸é¢å¤æ为ä¸ä¸ºç©ºè¿è¦å¤æå¯ç é¿åº¦æ¯å¦å¤§äº6ä½
if (inputValue.length < 6) {
errMsg = labelName + "å¿
须大äº6ä½";
$(this).next().text(errMsg);
flag = false;
return false;
}
}
// å¦ææ¯ææºç±»åï¼æ们éè¦å¤æææºçæ ¼å¼æ¯å¦æ£ç¡®
if (inputName === "mobile") {
// 使ç¨æ£å表达å¼æ ¡éªinputValueæ¯å¦ä¸ºæ£ç¡®çææºå·ç
if (!/^1[345678]\d{9}$/.test(inputValue)) {
// ä¸æ¯ææçææºå·ç æ ¼å¼
errMsg = labelName + "æ ¼å¼ä¸æ£ç¡®";
$(this).next().text(errMsg);
flag = false;
return false;
}
}
}
});
return flag;
}
function clearError(arg) {
// æ¸
空ä¹åçé误æ示
$(arg).next().text("");
}
// ä¸é¢é½æ¯æå®ä¹çå·¥å
·å½æ°
$.extend({
validate: function () {
$("form :submit").on("click", function () {
return check();
});
$("form :input[type!='submit']").on("focus", function () {
clearError(this);
});
}
});
})(jQuery);
- ä¼ åçæ件ï¼
- HTMLæ件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ç»å½æ ¡éªç¤ºä¾</title>
<style>
.login-form {
margin: 100px auto 0;
max-width: 330px;
}
.login-form > div {
margin: 15px 0;
}
.error {
color: red;
}
</style>
</head>
<body>
<div>
<form action="" class="login-form" novalidate>
<div>
<label for="username">å§å</label>
<input id="username" type="text" name="name" required autocomplete="off">
<span class="error"></span>
</div>
<div>
<label for="passwd">å¯ç </label>
<input id="passwd" type="password" name="password" required autocomplete="off">
<span class="error"></span>
</div>
<div>
<label for="mobile">ææº</label>
<input id="mobile" type="text" name="mobile" required autocomplete="off">
<span class="error"></span>
</div>
<div>
<label for="where">æ¥èª</label>
<input id="where" type="text" name="where" autocomplete="off">
<span class="error"></span>
</div>
<div>
<input type="submit" value="ç»å½">
</div>
</form>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="validate3.js"></script>
<script>
$.validate({"name":{"required": true}, "password": {"required": true, "minLength": 8}, "mobile": {"required": true}});
</script>
</body>
</html>
- JSæ件
"use strict";
(function ($) {
function check(arg) {
// å®ä¹ä¸ä¸ªæ å¿ä½ï¼è¡¨ç¤ºéªè¯éè¿è¿æ¯éªè¯ä¸éè¿
var flag = true;
var errMsg;
// æ ¡éªè§å
$("form input[type!=':submit']").each(function () {
var labelName = $(this).prev().text();
var inputName = $(this).attr("name");
var inputValue = $(this).val();
if (arg[inputName].required) {
// å¦ææ¯å¿
填项
if (inputValue.length === 0) {
// å¼ä¸ºç©º
errMsg = labelName + "ä¸è½ä¸ºç©º";
$(this).next().text(errMsg);
flag = false;
return false;
}
// å¦ææ¯å¯ç ç±»åï¼æ们就è¦å¤æå¯ç çé¿åº¦æ¯å¦å¤§äº6ä½
if (inputName === "password") {
// é¤äºä¸é¢å¤æ为ä¸ä¸ºç©ºè¿è¦å¤æå¯ç é¿åº¦æ¯å¦å¤§äº6ä½
if (inputValue.length < arg[inputName].minLength) {
errMsg = labelName + "å¿
须大äº"+arg[inputName].minLength+"ä½";
$(this).next().text(errMsg);
flag = false;
return false;
}
}
// å¦ææ¯ææºç±»åï¼æ们éè¦å¤æææºçæ ¼å¼æ¯å¦æ£ç¡®
if (inputName === "mobile") {
// 使ç¨æ£å表达å¼æ ¡éªinputValueæ¯å¦ä¸ºæ£ç¡®çææºå·ç
if (!/^1[345678]\d{9}$/.test(inputValue)) {
// ä¸æ¯ææçææºå·ç æ ¼å¼
errMsg = labelName + "æ ¼å¼ä¸æ£ç¡®";
$(this).next().text(errMsg);
flag = false;
return false;
}
}
}
});
return flag;
}
function clearError(arg) {
// æ¸
空ä¹åçé误æ示
$(arg).next().text("");
}
// ä¸é¢é½æ¯æå®ä¹çå·¥å
·å½æ°
$.extend({
validate: function (arg) {
$("form :submit").on("click", function () {
return check(arg);
});
$("form :input[type!='submit']").on("focus", function () {
clearError(this);
});
}
});
})(jQuery);