
文章插圖
Jquery操作Dom節(jié)點(diǎn)屬性和單選框多選框表單元素attr()和removeAttr()方法用于操作DOM節(jié)點(diǎn)的屬性:
// <div id="test-div" name="Test" start="1">...</div>var div = $('#test-div');div.attr('data'); // undefined, 屬性不存在div.attr('name'); // 'Test'div.attr('name', 'Hello'); // div的name屬性變?yōu)?Hello'div.removeAttr('name'); // 刪除name屬性div.attr('name'); // undefinedprop()方法和attr()類似,但是HTML5規(guī)定有一種屬性在DOM節(jié)點(diǎn)中可以沒有值,只有出現(xiàn)與不出現(xiàn)兩種,例如:
<input id="test-radio" type="radio" name="test" checked value="https://www.520longzhigu.com/diannao/1">等價于:
<input id="test-radio" type="radio" name="test" checked="checked" value="https://www.520longzhigu.com/diannao/1">attr()和prop()對于屬性checked處理有所不同:
var radio = $('#test-radio');radio.attr('checked'); // 'checked'radio.prop('checked'); // trueprop()返回值更合理一些 。不過,用is()方法判斷更好:
var radio = $('#test-radio');radio.is(':checked'); // true類似的屬性還有selected,處理時最好用is(‘:selected’) 。
操作表單對于表單元素,jQuery對象統(tǒng)一提供val()方法獲取和設(shè)置對應(yīng)的value屬性:
<body><!-- html --><input id="test-input" name="email" value="https://www.520longzhigu.com/diannao/test"><select id="test-select" name="city"><option value="https://www.520longzhigu.com/diannao/BJ" selected>Beijing</option><option value="https://www.520longzhigu.com/diannao/SH">Shanghai</option><option value="https://www.520longzhigu.com/diannao/SZ">Shenzhen</option></select><textarea id="test-textarea">Hello</textarea><div><label> r1 <input type="radio" name="r1" value="https://www.520longzhigu.com/diannao/r1" class="ra"></label><label> r2 <input type="radio" name="r1" value="https://www.520longzhigu.com/diannao/r2" class="ra"></label></div><div><button id="btn">修改radio選中狀態(tài)</button></div><script>varinput = $('#test-input'),select = $('#test-select'),textarea = $('#test-textarea');radio = $("input[name=r1]")console.log(input.val()); // 'test'input.val('abc@example.com'); // 文本框的內(nèi)容已變?yōu)閍bc@example.comconsole.log(select.val()); // 'BJ'select.val('SH'); // 選擇框已變?yōu)镾hanghaiconsole.log(textarea.val()); // 'Hello'textarea.val('Hi'); // 文本區(qū)域已更新為'Hi'//注意jq對象與dom對象的轉(zhuǎn)換console.log(radio) //jq對象console.log(radio[1]) //dom對象console.log(radio.get(0)==radio[0]) //true//初始狀態(tài):讓第一個radio選中radio.each((index,item)=>{console.log(item) //dom對象if($(item).val()=="r1"){ //使用val(),需要先轉(zhuǎn)換為jq對象$(item).prop("checked",true)}})//點(diǎn)擊btn修改radio選中狀態(tài) $("#btn").click(function(){// radioChecked = radio.find(":checked")//空,find是查找子元素radioChecked = radio.filter(":checked")//獲得選中的radio,filter是過濾當(dāng)前的元素radioUnChecked = radio.filter(":not(:checked)") //選擇未選中的radio,:not為反向選擇器// console.log(radioUnchecked)if(radioChecked){// radioChecked.prop("checked",false)radioUnChecked.prop("checked",true)}})</script></body>可見,一個val()就統(tǒng)一了各種輸入框的取值和賦值的問題 。但是radio有所不同,需要單獨(dú)使用prop()單獨(dú)設(shè)置 。當(dāng)然也可以使用attr()方法,使用prop()更好一些 。
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助:- 坐姿下拉 助你打造堅實背部
- 如何清空夫妻間的感情垃圾
- 坐姿下拉 塑造男人堅實背部
- 手機(jī)回收站刪除之后恢復(fù)方法 回收站清空后如何恢復(fù)文件
- 拉力器胸前下拉的作用是什么?
- 前端解決跨域的三種方法 jquery的each遍歷方法是干什么的
- 三種方法輕松搞定 一鍵清空歷史記錄
- 如何刪除歷史記錄 一鍵清空歷史記錄
- jquery顯示隱藏元素 jquery控制div顯示隱藏高度
- js輪播圖實現(xiàn)簡單代碼 輪播圖jquery代碼
