禁止编辑
1 2 3
| $('#id').find("*").each(function() { $(this).attr("disabled", "disabled"); });
|
点击事件
1 2 3 4 5
| $('.test').click(function() { $('.content').append('<li>keso</li>'); });
|
遍历元素
1 2 3
| $("li").each(function(){ alert($(this).text()) });
|
jq添加元素
jq样式处理
1 2 3 4 5 6
| if($(t).hasClass("fa-minus-square")){ $(t).parent().parent('.row').remove(); } if($(t).hasClass("fa-plus-square")){ $(t).removeClass("fa-plus-square").addClass("fa-minus-square"); }
|
jq获取值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| const getId = document.getElementById('getId');
console.log(getId.getAttribute("data-id")); console.log(getId.getAttribute("data-vice-id"));
getId.setAttribute("data-id","48"); console.log(getId.getAttribute("data-id"));
console.log(getId.dataset.id);
console.log(getId.dataset.viceId);
getId.dataset.id = "113"; getId.dataset.viceId--;
getId.dataset.id2 = "100";
getId.dataset.id2 = null; delete getId.dataset.id2;
var id = $("#getId").data("id");
$("#getId").data("id","100");
var id = $("#getId").attr("data-id");
$("#getId").attr("data-id","100");
|
js 数组处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| var myMap = {};
myMap['cityName'] = 'Beijing'; myMap['cityPopulation'] = 2152;
for(var key in myMap){ console.log(key + ' = ' + myMap[key]); }
var myMap = [];
myMap[] = 'Beijing'; myMap[] = 2152;
for (var i=0;i<myMap.length;i++){ console.log(i + ' ---> ' + myMap[i]); }
|
遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,array[i]); }
for(let index in array) { console.log(index,array[index]); }; var A = {a:1,b:2,c:3,d:"hello world"}; for(let k in A) { console.log(k,A[k]); }
array.forEach(v=>{ console.log(v); }); array.forEach(function(v){ console.log(v); });
|
数组转字符串
1 2 3 4 5
| var a, b,c; a = new Array(a,b,c,d,e); b = a.join('-'); c = a.join('');
|
字符串转数组
1 2 3 4
| var str = 'ab+c+de'; var a = str.split('+'); var b = str.split('');
|
### textarea字数限制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
function onfocusTextarea(t){ getTextareaNum(t); $(t).parent('div').find('p.num').show(); } function onblurTextarea(t){ $(t).parent('div').find('p.num').hide(); } function getTextareaNum(t){ var num = parseInt($(t).attr("maxlength")); $(t).val($(t).val().substring(0,num)); $(t).parent('div').find('.textareaNum').html($(t).val().length); }
|