实用代码块

1.验证某个元素为空
//方法一 
if (! $('#keks').html()) {}
// 方法二 
if ($('#keks').is(":empty")) {}

2.追加新元素到元素中去
$('#id').append('#new'); //append 附上

3.多个属性的选择
var elements = $('#someid input[type=sometype][value=somevalue]').get();

4.找到被选中option
$('#someElement').find('option:selected');

5.替换字符串中的值
var el = $('#id'); el.html(el.html().replace(/word/ig, ''));

6.定时器
setTimeout(function() { $('.mydiv').hide('blind', {}, 500) }, 5000);

7.克隆元素
var cloned = $('#somediv').clone();

8.判断元素是否可见
$(element).is(':visible');

9.把json格式转换成数组
var arrInputValues = new Array();
$("input[name='xxx']").each(function(){   
    arrInputValues.push($(this).val()); 
});

10.最简单生成节点对象方法
var select_object = $('<select>',{'id':'myselect','name':'myselect'});

11.遍历json和数组方法
for(var i=0;i<json.length;i++){
    json[i].name;
}
原文地址:https://www.cnblogs.com/zc123/p/5412519.html