选择器
$('.red').is('li'); //true,选择器,检测 class 为是否为 red
$('.red').is(function () { //true,方法,同上
return $(this).attr('title') == '列表 3'; //可以自定义各种判断
}));
$('li').eq(2).hasClass('red'); //和 is 一样,只不过只能传递 class
$('li').slice(0,2).css('color', 'red'); //前三个变成红色
$("div").find("p").end().get(0); //返回 div 的原生 DOM
$('div').contents().size(); //返回子节点(包括文本)数量
$('li').filter('.red').css('background','#ccc');
$('li').filter('.red, :first, :last').css('background','#ccc');
$('li').filter(function () {
return $(this).attr('class') == 'red' && $(this).attr('title') == '列表 3';
}).css('background', '#ccc');
$('a').parents().filter('div');//查找到父元素的同级元素的div
tr[datagrid-row-index="1"]
$("tr[datagrid-row-index=" + i + "]")
点击切换样式(简化)
//繁
$('div').click(function () {
$(this).toggleClass('red size');
if ($(this).hasClass('red')) {
$(this).removeClass('blue');
} else {
$(this).toggleClass('blue');//这里也可以 addClass
}
})
//简
$('div').click(function () {
$(this).toggleClass(function () {
return $(this).hasClass('red') ? 'blue' : 'red size';
});
});
点击计数
var flag = 1; //计数器
$('div').click(function () {
if (flag == 1) { //第一次点击
$(this).css('background', 'black');
flag = 2;
} else if (flag == 2) { //第二次点击
$(this).css('background', 'blue');
flag = 3
} else if (flag == 3) { //第三次点击
$(this).css('background', 'red');
flag = 1
}
});
点击div以外,div消失
$(document).click(function () {
$('#more ul,.delMenu').fadeOut();
});
$("#more td").click(function (e) {
e.stopPropagation();
})
//禁用鼠标右击,点击.tree-node时停止
$("div").bind("contextmenu", function () {
return false;
})
$('.tree-node').each(function () {
if ($(this).attr('item') == undefined) {} else {
$(this).mousedown(function (e) {
if (3 == e.which) {
$('.delMenu').show()
$('.delMenu').css({
'left': $(this).offset().left + 100,
'top': $(this).offset().top - 13
})
} else if (1 == e.which) {
$('.delMenu').hide()
}
})
}
})
$('.delMenu').click(function (e) {
e.stopPropagation();
$('.groupOperat a:last-child').click();
})
拖动.popup
var $dialog = $(".popup"),
mx = 0,
my = 0,
dx = 0,
dy = 0,
isDraging = false;
$(".popup").mousedown(function (e) {
e = e || window.event;
mx = e.pageX;
my = e.pageY;
dx = $dialog.offset().left;
dy = $dialog.offset().top;
isDraging = true;
});
$(document).mousemove(function (e) {
var e = e || window.event;
var x = e.pageX;
var y = e.pageY;
if (isDraging) {
var moveX = dx + x - mx;
var moveY = dy + y - my;
var maxX = $(window).width() - $dialog.width();
var maxY = $(window).height() - $dialog.height();
moveX = Math.min(Math.max(0, moveX), maxX);
moveY = Math.min(Math.max(0, moveY), maxY);
$dialog.css({
"left": moveX + 'px',
"top": moveY + 'px'
});
};
$(".popup").mouseup(function () {
isDraging = false;
});
})
$.param()将对象键值对转换为 URL 字符串键值对
var obj = {
name : 'Lee',
age : 100
};
alert($.param(obj));