jQuery常用方法
以下会列出一些jQuery常用的方法,包括下拉列表、单选框、复选框等的值的获取,常用的选择器,常用的选择器会用到的方法。
AJAX请求。
12345678910111213141516$.ajax({type: "get",async: true, //默认设置为true,所有请求均为异步请求。url: "xxxxx.php",data: {uname: 'user',con: 'test content'},dataType: "json", //xml、html、script、jsonp、textbeforeSend: function() {},complete: function() {},success: function(data) {alert(data)}error: function() {},});获取复选框checkbox,并判断是否选中。
12$("input[type='checkbox']").is(':checked');//true为选中。获取复选框checkbox的值。
1234var c_val =[];$('input[name="cbox"]:checked').each(function(){c_val.push($(this).val());});复选框checkbox 的 全选/反选/选择奇数。
123456789101112131415161718$("#btn1").click(function() {$("[name='checkbox']").attr("checked", 'true'); //全选});$("#btn2").click(function() {$("[name='checkbox']").removeAttr("checked"); //取消全选});$("#btn3").click(function() {$("[name='checkbox']:even").attr("checked", 'true'); //选中所有奇数});$("#btn4").click(function() {$("[name='checkbox']").each(function() { //反选if ($(this).attr("checked")) {$(this).removeAttr("checked");} else {$(this).attr("checked", 'true');}})})获取下拉列表select的值。
1$('#select').val();获取单选radio的值。
123$('input:radio:checked').val(); //方法 1$("input[type='radio']:checked").val(); //方法 2$("input[name='rad']:checked").val(); //方法 3设置第一个radio为选中。
12$('input:radio:first').attr('checked', 'checked');$('input:radio:first').attr('checked', 'true');根据索引值设置任意一个radio为选中。
12$('input:radio').eq(索引值).attr('checked', 'true'); //索引值=0,1,2....$("input[value='rd2']").attr('checked','true');jQuery对象和Dom对象的互相转化。
12345678// Dom对象 转为 jQuery对象$(document.getElementById("msg"));// jQuery对象 转为 Dom对象$('#msg')[0];$('div').eq(1)[0];$('div').get()[1];$('td')[5];同一个函数使用set和get。
123456789101112131415161718192021$("#msg").html(); //返回id为msg的元素节点的html内容。$("#msg").html("<b>new content</b>");//将"<b>new content</b>" 作为html串写入id为msg的元素节点内容中, 页面显示粗体的"new content"$("#msg").text(); //返回id为msg的元素节点的文本内容。$("#msg").text("<b>new content</b>");//将"<b>new content</b>" 作为普通文本串写入id为msg的元素节点内容中, 页面显示"<b>new content</b>""$("#msg").height(); //返回id为msg的元素的高度$("#msg").height("300"); //将id为msg的元素的高度设为300$("#msg").width(); //返回id为msg的元素的宽度$("#msg").width("300"); //将id为msg的元素的宽度设为300$("input").val(); //返回输入框的值$("input").val("test"); //将输入框的值设为test$("#msg").click(); //触发id为msg的元素的单击事件$("#msg").click(fn); //为id为msg的元素单击事件添加函数//类似的还有blur, focus, select, submit事件集合处理功能。
12345678$("p").each(function(i){this.style.color=['#f00','#0f0','#00f'][i]})//为索引分别为0,1,2的p元素分别设定不同的字体颜色。$("tr").each(function(i){this.style.backgroundColor=['#ccc','#fff'][i%2]})//实现表格的隔行换色效果$("p").click(function(){alert($(this).html())})//为每个p元素增加了click事件,单击某个p元素则弹出其内容功能拓展。
12345678//为jquery扩展了min,max两个方法$.extend({min: function(a, b){return a < b?a:b; },max: function(a, b){return a > b?a:b; }});//调用方式alert("a=10,b=20,max="+$.max(10,20)+",min="+$.min(10,20));修改元素的样式。
12345678910$("#msg").css("background"); //返回元素的背景颜色$("#msg").css("background","#ccc") //设定元素背景为灰色$("#msg").css({ color: "red", background: "blue" });//以名值对的形式设定样式$("#msg").height(300); //设定宽高$("#msg").width("200");$("#msg").addClass("select"); //为元素增加名称为select的class$("#msg").removeClass("select"); //删除元素名称为select的class$("#msg").toggleClass("select"); //如果存在(不存在)就删除(添加)名称为select的classjQuery的自定义事件。
hover(fn1, fn2): 一个模仿悬停事件的方法。移入时触发fn1,移出时触发fn2.
12345678$('tr').hover(function(){$(this).addClass("over");},function(){$(this).removeClass("over");});ready(fn): 当Dom全部载入就绪的时候会执行的一个函数,与$(fn)等价。不同于onload的是,onload需要图片加载完毕才会触发。
toggle(evenFn, oddFn): 每次点击时切换需要调用的函数,奇数次点击的时候触发evenFn,偶数次点击触发oddFn。
12345$("p").toggle(function(){$(this).addClass("selected");},function(){$(this).removeClass("selected");});trigger(event_type): 为匹配的元素触发某类事件。
1$('#btn').trigger('click');bind(event_type, fn), unbind(event_type): 事件的绑定与解除绑定。
12345678//为每个p元素添加单击事件$("p").bind("click", function() { alert($(this).text()); });//删除所有p元素上的所有事件$("p").unbind();//删除所有p元素上的单击事件$("p").unbind("click");