ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 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> ``` ```