## 一、監聽/取消事件
~~~
$('.box').on('click', selector, callback);
$('.box').off('click');
~~~
## 二、attr/prop屬性添加/刪除
~~~
// 自定義屬性
$('.box').attr('data-url', 'www.demo.com');
$('.box').removeAttr('data-url');
// 內置屬性
$('.box').prop('checked', true);
$('.box').removeProp('checked');
~~~
## 三、判斷數據
~~~
// 判斷是否是數字,返回true/false
$.isNumeric(1); // true
// 判斷是否是數組,返回true/false
$.isArray([]); // true
// 判斷元素是否在數組內,成功返回索引否則返回-1
$.inArray(1, [1,2,3]); // 0
// 判斷是否是空對象,返回true/false
$.isEmptyObject({}); // true
~~~
## 四、ajax相關
~~~
// ajax參數式
$.ajax({
// 請求method方式
type: 'post',
// 請求地址
url: 'demo.php',
// 請求參數
data: {name: 'tom'},
// 返回數據類型
dataType: 'json',
// 發送前
beforeSend: function() {
// 顯示loading
},
// 請求成功
success: function(res) {
console.log(res);
},
// 請求失敗
error: function() {
console.log('%c 請求接口失敗~', 'color: red');
},
// 請求完成(不管成功/失敗)
complete: function() {
// 隱藏loading
}
});
// ajax鏈式
$.ajax({
type: 'get',
url: 'demo.php'
}).done(function(res) {
console.log(res);
}).fail(function() {
console.log('請求失敗~');
});
// 取消ajax請求
var ajax1 = $.ajax({
type: 'get',
url: 'test.php',
success: function(res) {
console.log(res);
}
});
ajax1.abort();
// $.when方法,可用于所有異步請求完成
var ajax1 = $.ajax({
type: 'get',
url: 'test.php'
});
var ajax2 = $.ajax({
type: 'post',
url: 'test2.php'
});
var ajax3 = $.get('test3.php');
// then方式
$.when(ajax1, ajax2, ajax3).then(function(res1, res2, res3) {
console.log(res1[0]);
console.log(res2[0]);
console.log(res3[0]);
},function() {
console.log('%c 某個接口請求失敗~', 'color: red');
});
// 鏈式方式
$.when(ajax1, ajax2, ajax3).done(function(res1, res2, res3) {
console.log(res1[0]);
console.log(res2[0]);
console.log(res3[0]);
}).fail(function() {
console.log('%c 某個接口請求失敗~', 'color: red');
});
// ajax全局設置
$(document).ajaxSetup({
beforeSend: function() {},
dataFilter: function() {},
success: function() {},
error: function() {},
complete: function() {}
});
// $.Deferred方法
function req(url) {
var def = $.Deferred();
$.ajax({
type: 'get',
url: url,
success: function(res) {
def.resolve(res);
},
error: function() {
def.reject('請求失敗~');
}
});
return def;
}
req('demo.php').then(function(res) {
console.log(res);
return req('demo.php2');
}).then(function(res) {
console.log(res);
}, function() {
console.log('請求失敗~');
});
// 配合$.when
var $def = $.Deferred();
setTimeout(function() {
$def.resolve({name: 'tom'});
}, 1500);
$.when($def).then(function(res) {
console.log(res);
});
~~~
## 五、遍歷數據
~~~
// 數組
$.each([1,2,3], function(val, index) {
console.log(val, index);
});
// 對象
$.each({name: 'tom', age: 23}, function(k, v) {
console.log(k, v);
});
// 數組對象
$.each([{name: 'tom', age: 23}, {name: 'jack', age: 16}], function(val, index) {
console.log(JSON.stringify(val), index);
});
~~~
## 六、設置/獲取元素文本
~~~
<div class="box"><span>hello</span>world</div>
<script>
// 設置文本
$('.box').contents().filter(function() {
if(this.nodeType === 3) {
this.nodeValue = ',世界'
}
});
// 獲取文本
var text = $('.box').contents().filter(function() {
return this.nodeType === 3;
}).text();
console.log(text); // ,世界
</script>
~~~
## 七、$.extend方法
~~~
// 合并對象
var config = $.extend({}, {name: 'tom', age: 23}, {age: 24, gender: 'm'});
console.log(config); // {name: "tom", age: 24, gender: "m"}
// 擴展jQuery對象本身
$.extend({
checkbox: function() {
console.log(1);
}
});
$.checkbox(); // 1
~~~
## 八、$.fn.extend擴展jquery元素方法
~~~
<div class="box"></div>
<script>
$.fn.extend({
addText: function(text) {
this.text(text); // 這個this是jquery element context
}
});
$('.box').addText('hello');
</script>
~~~
## 九、獲取指定元素在集合中的索引
~~~
<div class="list">
<div class="item">a</div>
<div class="item2">1</div>
<div class="item">b</div>
<div class="item2">2</div>
<div class="item">c</div>
</div>
<script>
$('.list .item').click(function() {
console.log($(this).parent().find('.item').index($(this))); // 獲取指定元素在集合中的索引
});
</script>
~~~
## 十、事件委托on監聽scroll無效
~~~
$('body').on('scroll', '.box', function() { }); // 無效
// 需要這樣才行
$('.box').on('scroll', function() { });
~~~
- 事件
- mouse縮放與拖動
- drag拖動
- 事件兼容
- animation/transition
- canvas
- 改變圖片顏色
- html轉圖片
- 視頻操作
- 圖片縮放、水印、放大鏡
- 虛線
- 圓環進度條
- 形狀事件
- 圓角矩形
- 繪制注意
- arcTo與貝塞爾
- 橢圓及橢圓進度
- 五角星進度
- 常用圖形
- 計算顯示文本寬度
- 算法
- 幾何算法
- 地圖應用相關
- 運行符
- web安全
- 新窗口打開
- xss
- 分享交流
- php環境搭建及xhr交互
- node環境搭建及xhr交互
- node之socketio
- svg之入門介紹
- svg動畫
- vue之搜索聯想
- vue之登錄和echarts
- vue之組件交互與slot
- vue之loading
- vue之上傳進度
- webpack及cli
- 開發技巧
- 常用
- 移動端
- 錯誤處理
- 預加載
- 代理判斷
- 數組擴展
- 對象擴展
- 字符串擴展
- 語音播報
- 收集
- 文章/日記
- 框架/庫/插件
- 工具
- 學習網站
- 專業術語
- 正則
- 常用驗證
- 方法基礎
- es6擴展
- 深入實踐
- 快捷使用
- html
- css
- http協議
- http
- https
- socket
- 地圖/圖表
- mapbox
- echarts
- arcgis
- MapView及事件
- 添加WMS/WMTS層
- 增刪點線面
- 入門使用
- popup彈層
- 大數據處理
- 批量點
- 批量線
- 在線繪制
- GraphicLayer顯示/隱藏
- 動態改變位置
- 去除版權信息
- 添加控件
- Symbol
- 自定義path標記
- 圖片標記
- 文本標記
- 旋轉
- UI
- 自定義
- 3D地圖
- 創建實例
- basemap
- 底圖切換
- 自定義底圖
- 中心和范圍
- pupup彈層更新
- 坐標轉換
- 方向線
- leaflet
- amap
- 框架/類庫/腳手架
- vue
- 常見問題
- 組件框架
- vue-router
- 命名視圖
- url參數映射到prop
- sublime支持
- 隨手記
- 常用功能
- threejs
- 常用效果
- 其他特效
- requirejs
- 簡單使用
- jquery
- 方法擴展
- 使用筆記
- 組件擴展
- react
- 黨見問題
- 學習筆記
- 學習筆記-進階
- react-redux
- react-router
- redux
- 其他模塊說明
- 組件框架
- sublime支持
- gulp
- 安裝使用
- js壓縮
- css壓縮
- 組合使用
- copy文件
- 項目使用
- protobuf
- 入門
- layui
- 登錄驗證
- laydate
- 安裝工具
- yarn
- reactNative
- 入門介紹
- vueNative
- 入門介紹
- 版本控制
- git常用
- git擴展
- git問題
- git其他
- git擴展2
- 編輯器
- vscode
- atom
- webstorm
- 插件
- clipboard
- 奇淫巧技
- js
- 個性打印
- css
- 濾鏡效果
- 文本省略
- 當前色
- 新特性
- 花樣邊框效果
- 波紋效果
- 個性placeholder
- 偽元素內容
- 容器居中
- 知識點
- js
- 遞歸
- 沙箱
- 內存泄漏
- es6語法
- 變量介紹
- FileRead
- ajax
- web存儲
- css
- rem布局