# zepto/jquery
### 基础操作
zepto官方<https://cdn.bootcss.com/zepto/1.2.0/zepto.min.js>
zepto中文文档 <https://zeptojs.bootcss.com/>
jquery<https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js>
jquery手册[http://www.w3school.com.cn/jquery/jquery\_reference.asp](http://www.w3school.com.cn/jquery/jquery_reference.asp)
- dom操作
```
<pre class="calibre14">```
html text val分别用于获取设置html<span class="token">/</span>文本<span class="token">/</span>表单
$<span class="token2">(</span><span class="token4">"img"</span><span class="token2">)</span><span class="token2">.</span><span class="token1">attr</span><span class="token2">(</span><span class="token4">"src"</span><span class="token2">)</span><span class="token2">;</span>$<span class="token2">(</span><span class="token4">"img"</span><span class="token2">)</span><span class="token2">.</span><span class="token1">attr</span><span class="token2">(</span><span class="token4">"src"</span><span class="token2">,</span><span class="token4">"test.jpg"</span><span class="token2">)</span><span class="token2">;</span>设置返回属性
<span class="token1">addClass</span><span class="token2">(</span><span class="token2">)</span><span class="token">/</span><span class="token1">removeClass</span><span class="token2">(</span><span class="token2">)</span><span class="token">/</span><span class="token1">toggleClass</span><span class="token2">(</span><span class="token4">"selected"</span><span class="token2">)</span><span class="token2">;</span>添加<span class="token">/</span>删除<span class="token">/</span>切换类
<span class="token1">parent</span><span class="token2">(</span><span class="token2">)</span>父元素
<span class="token1">children</span><span class="token2">(</span><span class="token2">)</span> 子元素
<span class="token1">find</span><span class="token2">(</span><span class="token2">)</span> 查询元素
<span class="token1">siblings</span><span class="token2">(</span><span class="token2">)</span>兄弟元素
<span class="token1">next</span><span class="token2">(</span><span class="token2">)</span>下一个 <span class="token1">prev</span><span class="token2">(</span><span class="token2">)</span> 上一个
var data <span class="token">=</span> $<span class="token2">(</span><span class="token4">'#data'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">data</span><span class="token2">(</span><span class="token4">'title'</span><span class="token2">)</span><span class="token2">;</span>获取
$<span class="token2">(</span><span class="token4">'#data'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">removeData</span><span class="token2">(</span><span class="token4">'x'</span><span class="token2">)</span><span class="token2">;</span> 移除
$<span class="token2">(</span><span class="token4">'#data'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">data</span><span class="token2">(</span><span class="token4">'x'</span><span class="token2">,</span><span class="token2">{</span><span class="token4">"qq"</span><span class="token2">:</span><span class="token4">"89"</span><span class="token2">}</span><span class="token2">)</span><span class="token2">;</span>设置
dom对象相互转换
var dom <span class="token">=</span> $<span class="token2">(</span><span class="token4">"#ss"</span><span class="token2">)</span><span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">;</span>
var z<span class="token">=</span> $<span class="token2">(</span>document<span class="token2">.</span><span class="token1">getElementById</span><span class="token2">(</span><span class="token4">"id"</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span>
```
```
- 常用
```
<pre class="calibre14">```
$<span class="token2">(</span><span class="token4">'#nav li'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">click</span><span class="token2">(</span><span class="token5">function</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span>
$<span class="token2">(</span>this<span class="token2">)</span><span class="token2">.</span><span class="token1">addClass</span><span class="token2">(</span><span class="token4">'active'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">siblings</span><span class="token2">(</span><span class="token2">)</span><span class="token2">.</span><span class="token1">removeClass</span><span class="token2">(</span><span class="token4">'active'</span><span class="token2">)</span><span class="token2">;</span>
<span class="token2">}</span><span class="token2">)</span>
对象数组
$<span class="token2">.</span><span class="token1">each</span><span class="token2">(</span>arr<span class="token2">,</span><span class="token5">function</span><span class="token2">(</span>i<span class="token2">,</span>n<span class="token2">)</span><span class="token2">{</span>
<span class="token1">alert</span><span class="token2">(</span><span class="token4">"索引"</span><span class="token">+</span>i<span class="token">+</span><span class="token4">"对应的值"</span><span class="token">+</span>n<span class="token2">)</span><span class="token2">;</span>
<span class="token2">}</span><span class="token2">)</span><span class="token2">;</span>
数组获取
var img<span class="token">=</span><span class="token2">[</span><span class="token2">]</span><span class="token2">;</span>
$<span class="token2">(</span><span class="token4">'input[name="img[]"]'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">each</span><span class="token2">(</span><span class="token5">function</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span>img<span class="token2">.</span><span class="token1">push</span><span class="token2">(</span>$<span class="token2">(</span>this<span class="token2">)</span><span class="token2">.</span><span class="token1">val</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span><span class="token2">}</span><span class="token2">)</span><span class="token2">;</span>
on 绑定多种事件 click dblclick change select<span class="token2">,</span>submit
tap手机单机 doubleTap手机双击 longTap 手机长按
swipe<span class="token2">,</span> swipeLeft<span class="token2">,</span> swipeRight<span class="token2">,</span> swipeUp<span class="token2">,</span> swipeDown 手机滑动
<span class="token1">hover</span><span class="token2">(</span>func1<span class="token2">,</span>func2<span class="token2">)</span> 鼠标移入调用func1函数,移出调用func2函数
focus<span class="token">/</span>blur 鼠标聚焦<span class="token">/</span>失去焦点触发事件(不支持冒泡)
$<span class="token2">(</span><span class="token4">"form"</span><span class="token2">)</span><span class="token2">.</span><span class="token1">on</span><span class="token2">(</span><span class="token4">"submit"</span><span class="token2">,</span> <span class="token5">function</span><span class="token2">(</span>event<span class="token2">)</span> <span class="token2">{</span>
<span class="token6">//表单提交</span>
<span class="token2">}</span><span class="token2">)</span><span class="token2">;</span>
$<span class="token2">(</span>document<span class="token2">)</span><span class="token2">.</span><span class="token1">on</span><span class="token2">(</span><span class="token4">'click'</span><span class="token2">,</span><span class="token4">'.list'</span><span class="token2">,</span><span class="token5">function</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span>
<span class="token6">//动态创建也生效</span>
<span class="token2">}</span><span class="token2">)</span>
$<span class="token2">(</span><span class="token4">'#con'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">focus</span><span class="token2">(</span><span class="token2">)</span><span class="token2">.</span><span class="token1">val</span><span class="token2">(</span><span class="token4">"文字右边获得焦点"</span><span class="token2">)</span><span class="token2">;</span>
<span class="token1">fadeIn</span><span class="token2">(</span><span class="token2">)</span>淡入显示
<span class="token1">fadeOut</span><span class="token2">(</span><span class="token2">)</span>淡出 隐藏
<span class="token1">hide</span><span class="token2">(</span><span class="token2">)</span> 和 <span class="token1">show</span><span class="token2">(</span><span class="token2">)</span> 方法来隐藏和显示
$<span class="token2">(</span><span class="token4">'dt'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">click</span><span class="token2">(</span><span class="token5">function</span><span class="token2">(</span>e<span class="token2">)</span><span class="token2">{</span>
<span class="token6">//防止冒泡</span>
e<span class="token2">.</span><span class="token1">stopPropagation</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>
<span class="token2">}</span><span class="token2">)</span><span class="token2">;</span>
```
```
刷新表单数据 `$('#form1')[0].reset()` 清空表单不能是隐藏域
表单验证
```
<pre class="calibre14">```
onsubmit<span class="token">=</span><span class="token4">"return form2()"</span>
const form2<span class="token">=</span><span class="token2">(</span><span class="token2">)</span><span class="token">=</span><span class="token">></span><span class="token2">{</span>
<span class="token2">}</span>
```
```
- 表单取值
```
<pre class="calibre14">```
var select <span class="token">=</span> $<span class="token2">(</span><span class="token4">"#select option:selected"</span><span class="token2">)</span><span class="token2">.</span><span class="token1">val</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">;</span><span class="token6">//select单选</span>
var select <span class="token">=</span> <span class="token2">[</span><span class="token2">]</span> <span class="token2">;</span><span class="token6">//select多选</span>
$<span class="token2">(</span><span class="token4">"#select option:selected"</span><span class="token2">)</span><span class="token2">.</span><span class="token1">each</span><span class="token2">(</span><span class="token5">function</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span>
select<span class="token2">.</span><span class="token1">push</span><span class="token2">(</span>$<span class="token2">(</span>this<span class="token2">)</span><span class="token2">.</span><span class="token1">val</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">;</span>
<span class="token2">}</span><span class="token2">)</span> <span class="token2">;</span>
var radio <span class="token">=</span> $<span class="token2">(</span><span class="token4">'input[name="radio"]:checked'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">val</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>
<span class="token6">//单选</span>
$<span class="token2">(</span>`input<span class="token2">[</span>name<span class="token">=</span><span class="token4">'sex'</span><span class="token2">]</span><span class="token2">[</span>value<span class="token">=</span><span class="token4">'1'</span><span class="token2">]</span>`<span class="token2">)</span><span class="token2">.</span><span class="token1">attr</span><span class="token2">(</span><span class="token4">'checked'</span><span class="token2">,</span><span class="token4">'true'</span><span class="token2">)</span><span class="token2">;</span> 设置单选选中
var checkbox<span class="token">=</span><span class="token2">[</span><span class="token2">]</span><span class="token2">;</span>
<span class="token6">//复选</span>
$<span class="token2">(</span><span class="token4">'input[name="checkbox[]"]:checked'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">each</span><span class="token2">(</span><span class="token5">function</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span>
checkbox<span class="token2">.</span><span class="token1">push</span><span class="token2">(</span>$<span class="token2">(</span>this<span class="token2">)</span><span class="token2">.</span><span class="token1">val</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">;</span>
<span class="token2">}</span><span class="token2">)</span><span class="token2">;</span>
设置复选选中
<span class="token1">foreach</span><span class="token2">(</span><span class="token2">[</span><span class="token3">1</span><span class="token2">,</span><span class="token3">2</span><span class="token2">,</span><span class="token3">3</span><span class="token2">,</span><span class="token3">4</span><span class="token2">]</span><span class="token2">,</span><span class="token5">function</span><span class="token2">(</span>v<span class="token2">)</span><span class="token2">{</span>
$<span class="token2">(</span>`input<span class="token2">[</span>name<span class="token">=</span><span class="token4">'tezhen'</span><span class="token2">]</span><span class="token2">[</span>value<span class="token">=</span><span class="token4">'${v}'</span><span class="token2">]</span>`<span class="token2">)</span><span class="token2">.</span><span class="token1">attr</span><span class="token2">(</span><span class="token4">'checked'</span><span class="token2">,</span><span class="token4">'true'</span><span class="token2">)</span><span class="token2">;</span>
<span class="token2">}</span><span class="token2">)</span>
<span class="token"><</span>input type<span class="token">=</span><span class="token4">"checkbox"</span> onclick<span class="token">=</span><span class="token4">"var ck=this.checked;$('input[name=\'checkbox[]\']').each(function(){this.checked = ck});"</span><span class="token">></span><span class="token6">//全选取消</span>
```
```
- ajax
```
<pre class="calibre16">```
$<span class="token2">.</span><span class="token1">post</span><span class="token2">(</span><span class="token4">"1.php"</span><span class="token2">,</span><span class="token2">{</span>a<span class="token2">:</span><span class="token3">1</span><span class="token2">,</span>b<span class="token2">:</span><span class="token4">"2"</span><span class="token2">}</span><span class="token2">,</span><span class="token5">function</span><span class="token2">(</span>res<span class="token2">)</span><span class="token2">{</span>
<span class="token1">alert</span><span class="token2">(</span>JSON<span class="token2">.</span><span class="token1">stringify</span><span class="token2">(</span>res<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span>
<span class="token2">}</span><span class="token2">,</span><span class="token4">'json'</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//POST返回默认json,html,jsonp,xml,text,script</span>
$<span class="token2">.</span><span class="token1">get</span><span class="token2">(</span><span class="token4">"1.php"</span><span class="token2">,</span><span class="token2">{</span>a<span class="token2">:</span><span class="token3">1</span><span class="token2">,</span>b<span class="token2">:</span><span class="token4">"2"</span><span class="token2">}</span><span class="token2">,</span><span class="token5">function</span><span class="token2">(</span>res<span class="token2">)</span><span class="token2">{</span>
<span class="token1">alert</span><span class="token2">(</span>JSON<span class="token2">.</span><span class="token1">stringify</span><span class="token2">(</span>res<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span>
<span class="token2">}</span><span class="token2">,</span><span class="token4">'json'</span><span class="token2">)</span><span class="token2">;</span>
$<span class="token2">(</span><span class="token4">"#form1"</span><span class="token2">)</span><span class="token2">.</span><span class="token1">serialize</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//直接提交</span>
$<span class="token2">.</span><span class="token1">ajax</span><span class="token2">(</span><span class="token2">{</span>
<span class="token6">//跨域</span>
url<span class="token2">:</span> <span class="token4">"http://shanliwawa.top"</span><span class="token2">,</span>
type<span class="token2">:</span> <span class="token4">"GET"</span><span class="token2">,</span>
dataType<span class="token2">:</span> <span class="token4">"jsonp"</span><span class="token2">,</span>
jsonp<span class="token2">:</span><span class="token4">"callback"</span><span class="token2">,</span>
success<span class="token2">:</span> <span class="token5">function</span> <span class="token2">(</span>data<span class="token2">)</span> <span class="token2">{</span>
<span class="token2">}</span>
<span class="token2">}</span><span class="token2">)</span><span class="token2">;</span>
php
echo $_GET<span class="token2">[</span><span class="token4">'callback'</span><span class="token2">]</span> <span class="token2">.</span> <span class="token4">'('</span> <span class="token2">.</span> $json <span class="token2">.</span> <span class="token4">')'</span><span class="token2">;</span>
$<span class="token2">.</span><span class="token1">ajax</span><span class="token2">(</span><span class="token2">{</span>
type<span class="token2">:</span><span class="token4">"get"</span><span class="token2">,</span>
url<span class="token2">:</span><span class="token4">''</span><span class="token2">,</span>
data<span class="token2">:</span> <span class="token2">{</span><span class="token2">}</span><span class="token2">,</span>
dataType<span class="token2">:</span><span class="token4">'json'</span><span class="token2">,</span>
timeout<span class="token2">:</span><span class="token3">10000</span><span class="token2">,</span>
beforeSend<span class="token2">:</span><span class="token5">function</span><span class="token2">(</span>xhr<span class="token2">)</span><span class="token2">{</span>
$<span class="token2">(</span><span class="token4">'.loading'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">show</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>
<span class="token2">}</span><span class="token2">,</span>
success<span class="token2">:</span><span class="token5">function</span><span class="token2">(</span>rs<span class="token2">)</span><span class="token2">{</span>
$<span class="token2">(</span><span class="token4">'.loading'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">hide</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>
var tpl <span class="token">=</span> document<span class="token2">.</span><span class="token1">getElementById</span><span class="token2">(</span><span class="token4">'tpl'</span><span class="token2">)</span><span class="token2">.</span>innerHTML<span class="token2">;</span>
var desc<span class="token">=</span><span class="token1">tpl</span><span class="token2">(</span>tpl<span class="token2">,</span>rs<span class="token2">)</span><span class="token2">;</span>
$<span class="token2">(</span><span class="token4">"#rank-list"</span><span class="token2">)</span><span class="token2">.</span><span class="token1">append</span><span class="token2">(</span>desc<span class="token2">)</span><span class="token2">;</span>
<span class="token2">}</span><span class="token2">,</span>
error<span class="token2">:</span><span class="token5">function</span><span class="token2">(</span>xhr<span class="token2">)</span><span class="token2">{</span>
<span class="token1">alert</span><span class="token2">(</span><span class="token4">'ajax出错'</span><span class="token2">)</span><span class="token2">;</span>
<span class="token2">}</span><span class="token2">,</span>
<span class="token2">}</span><span class="token2">)</span><span class="token2">;</span>
```
```
- 简介
- 第一章 数据库
- Mysql/mariadb
- 函数
- 基础
- 增删改索引
- 标准查询
- 高级查询
- TIDB集群mysql解决方案
- Redis
- 语言基础
- 5种数据类型
- 其他类型
- Sqlite
- 语言基础
- 常用查询
- 第二章 PHP
- 语言基础
- 第一课 流程控制和运算
- 第二课 数组
- 第三课 日期时间
- 第四课 常用函数
- 第五课 字符串
- 第六课 文件操作
- 第七课 面向对象
- 第八课 正则表达式
- 第九课 图片处理生成
- 第十课 curl/memche
- 第十一课 mysql和pdo
- 第十三课 cookie和session
- 第十四课 xml操作
- 第十五课 php5.3+新特性
- 第十六课 php7+
- 第十七课 密码安全
- 废弃函数
- php命令行
- redis应用
- 算法
- 排序算法
- 基础算法
- 无限级分类
- 自定义函数Fn
- 查找算法
- 自定义函数数据函数fn
- laravel
- 路由
- 常用语句
- 数据库
- dingo/api
- Yii2
- 控制器
- 常用类
- 数据库
- redis
- thinkphp6
- TP6文档
- TP6插件
- dedecms
- 织梦标签大全
- 数据库操作
- 内置函数和定义函数
- 织梦核心改动
- 织梦插件/底层标签开发
- PHP相关工具
- composer
- php开发环境phpenv
- Phpstorm使用
- windows编译php扩展
- PHP开源库
- 开源项目管理禅道
- sns_auth
- php-casbin权限控制
- php-jwt
- 微信SDKeasywechat
- querylist采集库
- workerman
- Box/Spout处理excel和csv
- dll扩展
- redis/memche/xdebug
- redis
- Lua
- php_xlswriter
- event
- swoole
- 常用代码库
- 微擎框架
- 第一课全局变量
- 第二课常用函数
- 第三课自定义微擎独有函数
- 第四课数据库操作
- 第五课微信端回复
- 第六课微擎高级操作
- 第八课global函数列表
- mainfest.xml详解
- js方法
- 人人商城
- 第一课model解读
- 第二课常用语句解读
- 第三课常用js解读
- 第四课附录常见问题
- 第五课附录处理报表|支付
- 常用JSON状态码
- 第三章 JavaScript
- js基础
- 浏览器对象
- 语言基础
- html5接口
- ES6新语法
- vue
- 基础语法
- 京东vueUI组件
- uniapp
- 组件开发规范
- nodejs
- 基础知识
- 安装node
- nvm不同版本node切换
- js常用标准库
- zepto/jquery
- weui
- js图标库
- elementUI
- validator表单验证
- layer弹出层
- requirejs
- wow动画
- 动画animate
- swiper4
- 百度编辑器
- flyio/axios/qs
- jquery.form
- bootstrap3
- clipboard复制
- slideout侧滑
- imagehover.css图片悬停动画
- webpack打包
- Bulma UI框架
- store 客户端存储
- lottie动画创建库
- sweetalert
- js自定义函数
- 常见JSSDK
- 微信公众号JSSDK
- 腾讯地图jssdk
- 微信小程序
- 第四章 编程语言
- markdown语言
- Dart语言
- Dart语言基础
- Flutter框架
- Lua语言
- 字符串,数组,表
- 自定义方法
- go语言
- 第1.1语言基本语法
- 第1.2流程控制
- 第1.3函数
- 第1.4结构体
- 第1.5接口
- 第1.6包
- go语言框架Gin
- CSS3语言
- CSS与CSS3
- 选择符
- 属性
- css3
- loading动画
- HTML5语言
- less
- sass
- C#
- 基础知识
- 函数
- 第五章 开发工具
- git
- nginx/apache服务器
- Linux常用操作
- crontab定时任务
- 注册表与cmd
- 阿里云ECS
- frp穿透和ssl续期
- 宝塔安装
- 树莓派
- 浏览器模拟
- 火狐/chrome常用插件
- WSL安装使用
- mac brew和终端命令
- win10相关