方法
注意:引入的jQuey插件(即.js文件)需要写在jQuery引入语句之后,因为现有jQuery才能有jQuery插件。
自定义简单插件
需求: 1.给$添加4个工具方法:
- min(a, b) :返回较小的值
- max( c, d) :返回较大的值
- LeftTrim() :去掉字符串左边的空格
- rightTrim() :去掉字符串右边的空格
2.给jQuery对象添加3个功能方法: - checkALL():全选
- unCheckALL() :全不选
- reverseCheck() :全反选
<!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.0">
<title>Document</title>
</head>
<body>
<form method="post" action="">
<input type="checkbox" name="items" value="足球"/>足球
<input type="checkbox" name="items" value="篮球"/>篮球
<input type="checkbox" name="items" value="羽毛球"/>羽毛球
<input type="checkbox" name="items" value="乒乓球"/>乒乓球<br/>
<input type="button" id="checkedA11Btn" value="全选"/>
<input type="button" id="checkedNoBtn" value="全不选"/>
<input type="button" id="checkedRevBtn" value="反选"/>
<input type="button" id="sendBtn" value="提交"/>
</form>
<script type='text/javascript' src='./jquery库/jquery-1.12.4.js'></script>
<script type='text/javascript' src='./jquery库/myjQuery.js'></script>
<script>
console.log($.min(3,5),$.max(3,5))
var str=" baidu "
console.log($.leftTrim(str))
console.log($.rightTrim(str))
var $items = $(':checkbox[name=items]')
$('#checkedA11Btn').click(function (){
$items.checkAll()
})
$('#checkedNoBtn').click(function (){
$items.unCheckAll()
})
$('#checkedRevBtn').click(function (){
$items.reverseCheck()
})
</script>
</body>
</html>
myjQuery.js
(function () {
$.extend({
min:function (a,b){
return a<b?a:b
},
max:function (a,b){
return a>b?a:b
},
leftTrim: function (str) {
return str.replace(/^\s+/,'')
},
rightTrim: function (str) {
return str.replace(/\s+$/,'')
}
})
$.fn.extend({
checkAll:function (){
this.prop('checked',true)
},
unCheckAll:function (){
this.prop('checked',false)
},
reverseCheck: function () {
this.each(function (){
this.checked =!this.checked
})
}
})
})()
jQuery已有插件
jQuery插件:基于jQuery编写的扩展库。 jQuery插件很多。
jquery-validation
作用:表单验证的插件。
下载
下载地址:https://github.com/jquery-validation/jquery-validation/releases/tag/1.19.3
使用
(根据刚才的下载包中的 index.html学习如何使用该插件) 新建自己的项目文件夹将需要的jQuery和jQueryValidate文件夹从刚才下载的文件夹中进行引入。 将上面两个文件夹复制到自己的项目文件夹中: 在自己的html页面中引入这两个js文件(注意要先引入jQuery文件,在引入jQuery的插件文件)
<!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.0" />
<title>Document</title>
<style>
.error {
color: red;
}
</style>
</head>
<body>
<form id="myForm" action="XXX">
<p>
用户名:(必须,最少6位)<input
name="username"
type="text"
required
minlength="6"
/>
</p>
<p>
密码:(必须,6-8位)<input
id="password"
name="pwd1"
type="password"
required
minlength="6"
maxlength="8"
/>
</p>
<p>
确认密码:(与密码一样)<input
name="pwd2"
type="password"
equalTo="#password"
/>
</p>
<p><input type="submit" value="注册" /></p>
</form>
<script type="text/javascript" src="./jquery-1.7.2.js"></script>
<script type="text/javascript" src="./jquery.validate.js"></script>
<script>
$("#myForm").validate({
messages:{
username:{
required:'用户名是必须的',
minlength:'用户名至少为6位'
},
pwd1:{
required:'密码是必需的',
minlength:'密码至少为6位',
maxlength:'密码最多为8位'
},
pwd2:'必须与密码相同'
}
});
</script>
</body>
</html>
实现表单验证
jquery UI
下载
下载地址:https://jqueryui.com/download/ 选择对应的版本进行下载:
使用
jQueryUI有自己专门的样式库jquery-ui.css ,同时将jquery.js,jquery-ui.js文件从刚才加载的内容中拷贝到自己的项目文件夹中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQueryUI</title>
<link rel="stylesheet" href="./jquery-ui.css">
</head>
<body>
<h2>手风琴</h2>
<div id="accordion">
<h3>选项一</h3>
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquid, nihil error praesentium neque quos, asperiores eos libero quaerat enim officiis eaque cupiditate quis doloribus minus porro modi ut sint similique.</div>
<h3>选项二</h3>
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquid, nihil error praesentium neque quos, asperiores eos libero quaerat enim officiis eaque cupiditate quis doloribus minus porro modi ut sint similique.</div>
<h3>选项三</h3>
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquid, nihil error praesentium neque quos, asperiores eos libero quaerat enim officiis eaque cupiditate quis doloribus minus porro modi ut sint similique.</div>
</div>
<h2>自动匹配</h2>
<div>
<input id="autocomplete" title="type "y;a"y;">
</div>
<h2>选项卡</h2>
<div id="tabs">
<ul>
<li><a href="#tabs-1">First</a></li>
<li><a href="#tabs-2">Second</a></li>
<li><a href="#tabs-3">Third</a></li>
</ul>
<div id="tabs-1">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div id="tabs-2">Phasellus mattis tincidunt nibh. Cras orci urna, blandit id, pretium vel, aliquet ornare, felis. Maecenas scelerisque sem non nisl. Fusce sed lorem in enim dictum bibendum.</div>
<div id="tabs-3">Nam dui erat, auctor a, dignissim quis, sollicitudin eu, felis. Pellentesque nisi urna, interdum eget, sagittis et, consequat vestibulum, lacus. Mauris porttitor ullamcorper augue.</div>
</div>
<script type='text/javascript' src='./jquery.js'></script>
<script type='text/javascript' src='./jquery-ui.js'></script>
<script>
$('#accordion').accordion()
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#autocomplete" ).autocomplete({
source: availableTags
});
$('#tabs').tabs()
</script>
</body>
</html>
laydate——(基于原生JS)
下载
下载地址:https://www.layuion.com/laydate/
使用
将laydate整个文件夹拷贝到你的项目文件夹中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>layDate</title>
<script type='text/javascript' src='./laydate/laydate.js'></script>
</head>
<body>
<input type="text" class="demo-input" placeholder="请选择日期" id="test1">
<script>
laydate.render({
elem: '#test1'
});
</script>
</body>
</html>
|