Jquery操作DOM元素方法詳解
一、文本輸入框: text
<input type="text" value="99.com" size=12 id="input1" />
1、獲取文本值:
$("#input1").val();
2、選中文本:
$("#input1").select();
3、禁用、啟用文本框
$("#input1"].attr("disabled",true);
4、只讀、取消只讀:
$("#input1"].attr("readonly",true);
二、單選框: radio
<intput type="radio" name="sex" value="0"/>男 <intput type="radio" name="sex" value="1"/>女
1、得到單選框的選中項的值:
$("input[type=radio][name=sex]:checked").val();
2、勾選單選框的項:
$("input:radio][name=sex][value=0]).prop("checked",true); --或者 $("input:radio][name=sex"]).val(["0"]);
3、判斷是否勾選:
$("input:radio][name=sex][value=0]).prop("checked")==true;
4、禁用、啟用單選框:
$("input:radio][name=sex].prop("disabled",true); --或 $("input:radio][name=sex].removeAttr("disabled");
三、復(fù)選框: checkbox
<intput type="checkbox" name="sex" value="0"/>男 <intput type="checkbox" name="sex" value="1"/>女
1、得到所有checked中項的值:
$("input[type=checkbox][name=sex]:checked").each(function(i,n){ //由于復(fù)選框一般選中的是多個,所以可以循環(huán)輸出 alert($(n).val()); });
2、勾選復(fù)選框的項:
$("input:checkbox][name=sex][value=0]).prop("checked",true); --或者 $("input:checkbox][name=sex"]).val(["0"]);
3、判斷是否勾選:
$("input:radio][name=sex][value=0]).prop("checked")==true;
4、禁用、啟用復(fù)選框:
$("input:checkbox][name=sex].prop("disabled",true); --或 $("input:checkbox][name=sex].removeAttr("disabled");
5、全選、全不選
$("input:checkbox][name=sex].prop("checked",true); --或 $("input:checkbox][name=sex].removeAttr("checked");
6、反選
$("input[type=checkbox][name=sex]").each(function(i,n){ $(this).attr('checked',!$(this).attr('checked')==true); });
四、下拉框 select
<select name="select" id="sel"> <option value="00">a </option> <option value="11">b </option> <option value="22">c </option> </select>
1、 獲取選擇項的值:
$("#sel").val();
2、獲取選擇項的文本:
$("#sel option:selected").text();
3、選中第二個項:
$("#sel").val("11"); $("#sel").val(["11"]); $("#sel").val("b"); $("#sel").val(["bb"]); $("#sel option[value="11"]").attr("selected",true); $("#sel option:contains('b')").attr("selected",true);
4、禁用、啟用下拉框:
$("#sel"].prop("disabled",true); --或 $("#sel").removeAttr("disabled");
5、清空項:
$("#sel").empty(); $("#sel").html('');
6、添加項:
$("#sel").append("<option value='33'>dd</option>"); $("#sel").prepend("<option value=''>請選擇</option>"); //為Select插入一個Option(第一個位置)
7、移除選擇項:
$("#sel option:selected").remove();
五、多選下拉框 select-multiple
<select name="selectMul" id="selMul" size=4 multiple="multiple">//size列表框的高度 <option value="00">a </option> <option value="11">b </option> <option value="22">c </option> </select>
1、 獲取選擇項的值:
$("#selMul").val();//如果多選,返回一個數(shù)組val().join(‘,')
2、獲取選擇項的個數(shù):
$("#selMul option").length
3、獲取選擇項的文本:
$("#selMul option:selected").each(function(i,n){ $(this).text(); });
4、選中第二個項:
$("#selMul ").val("11"); $("#selMul ").val(["11","22"]); $("#selMul ").val("b"); $("#selMul ").val(["aa","bb"]); $("#selMul option[value="11"]").attr("selected",true); $("#selMul option:contains('b')").attr("selected",true);
5、禁用、啟用下拉框:
$("#selMul"].prop("disabled",true); --或 $("#selMul").removeAttr("disabled");
6、清空項:
$("#selMul").empty(); $("#selMul").html('');
7、添加項:
$("#selMul").append("<option value='33'>dd</option>"); $("#selMul").prepend("<option value=''>請選擇</option>"); //為Select插入一個Option(第一個位置)
8、移除選擇項:
$("#selMul option:selected").remove();
9、全選、全不選
$("#selMul option).attr("selected",true); --或 $("("#selMul option).removeAttr("selected");
10、反選
$("#selMul option).each(function(i,n){ $(this).attr(‘selected',!$(this).attr(‘selected')==true); });
到此這篇關(guān)于Jquery操作DOM元素的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一個超簡單的jQuery回調(diào)函數(shù)例子(分享)
下面小編就為大家?guī)硪黄粋€超簡單的jQuery回調(diào)函數(shù)例子(分享) 。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08jQuery插件實現(xiàn)的日歷功能示例【附源碼下載】
這篇文章主要介紹了jQuery插件實現(xiàn)的日歷功能,結(jié)合完整實例形式分析了jQuery datepicker插件實現(xiàn)日歷功能的相關(guān)操作技巧,需要的朋友可以參考下2018-09-09Jquery 模板數(shù)據(jù)綁定插件的使用方法詳解
本篇文章是對在Jquery中模板數(shù)據(jù)綁定插件的使用方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07jQuery學(xué)習(xí)筆記之jQuery+CSS3的瀏覽器兼容性
這篇文章主要介紹了jQuery學(xué)習(xí)筆記之jQuery+CSS3的瀏覽器兼容性的相關(guān)資料,需要的朋友可以參考下2015-01-01