JQuery的attr 與 val區(qū)別
.attr(attributeName)
attributeName:需要獲取屬性的名稱。
獲取匹配集中第一個(gè)元素的屬性值。1.6中attr返回屬性的值為undefined,如果沒有設(shè)置(set)。另外,.attr不應(yīng)該在普通對(duì)象、數(shù)組(array)、窗口(window)或者文檔中(document)。如果需要獲取或者設(shè)置DOM屬性,則應(yīng)該使用.prop()方法。
使用.attr方法獲取元素屬性的值有兩個(gè)主要優(yōu)點(diǎn):
方便(Convenience):這個(gè)方法可以在JQuery對(duì)象上直接調(diào)用和串聯(lián)別的JQuery的方法。
跨瀏覽器的一致性(Cross-browser consistency):有報(bào)告說一些屬性值在跨瀏覽器時(shí)的不一致性,甚至在同一瀏覽器的不同版本上也有不一致性。.attr減少這種不一致性
.val()
獲取匹配集中第一個(gè)元素當(dāng)前的值。
.val()就去主要用來獲取表單中元素的值,例如input, select 或者textarea。
不同
<input data-name="user" id="name" value="aaaa" />
?$('#name').val() ;/* 'aaaa'*/
$('#name').attr('data-name'); /*user*/
下面通過一段代碼給大家介紹jQuery attr("value") 和 val的區(qū)別
//2509行
if ( !getSetInput || !getSetAttribute ) {
jQuery.attrHooks.value = {
get: function( elem, name ) {
var ret = elem.getAttributeNode( name );
return jQuery.nodeName( elem, "input" ) ?
// Ignore the value *property* by using defaultValue
elem.defaultValue :
ret && ret.specified ? ret.value : undefined;
},
}
這邊返回值的邏輯判斷有變化
jQuery.nodeName( elem, "input" ) ?elem.defaultValue :ret && ret.specified ? ret.value :undefined; // Ignore the value *property* by using defaultValue
要我們使用defaultValue.
JavaScript
attrHooks: {
type: {
set: function( elem, value ) {
if ( !jQuery.support.radioValue && value === "radio" && jQuery.nodeName(elem, "input") ) {
// Setting the type on a radio button after the value resets the value in IE6-9
// Reset value to default in case type is set after value during creation
var val = elem.value;
elem.setAttribute( "type", value );
if ( val ) {
elem.value = val;
}
return value;
}
}
}
},
而1.8.3代碼如下
JavaScript
//2361行
attrHooks: {
type: {
set: function( elem, value ) {
// We can't allow the type property to be changed (since it causes problems in IE)
if ( rtype.test( elem.nodeName ) && elem.parentNode ) {
jQuery.error( "type property can't be changed" );
} else if ( !jQuery.support.radioValue && value === "radio" && jQuery.nodeName(elem, "input") ) {
// Setting the type on a radio button after the value resets the value in IE6-9
// Reset value to it's default in case type is set after value
// This is for element creation
var val = elem.value;
elem.setAttribute( "type", value );
if ( val ) {
elem.value = val;
}
return value;
}
}
},
// Use the value property for back compat
// Use the nodeHook for button elements in IE6/7 (#1954)
value: {
get: function( elem, name ) {
if ( nodeHook && jQuery.nodeName( elem, "button" ) ) {
return nodeHook.get( elem, name );
}
return name in elem ?
elem.value :
null;
},
set: function( elem, value, name ) {
if ( nodeHook && jQuery.nodeName( elem, "button" ) ) {
return nodeHook.set( elem, value, name );
}
// Does not return so that setAttribute is also used
elem.value = value;
}
}
},
可見1.9刪除了attrHooks.value方法導(dǎo)致非IE的attr("value")為undefined或默認(rèn)值,而IE的attr("value")為""或默認(rèn)值
相關(guān)文章
創(chuàng)建公共調(diào)用 jQuery Ajax 帶返回值
請(qǐng)求Ajax 帶返回值,并彈出提示框提醒的實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-08-08
jQuery實(shí)現(xiàn)ajax回調(diào)函數(shù)帶入?yún)?shù)的方法示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)ajax回調(diào)函數(shù)帶入?yún)?shù)的方法,結(jié)合實(shí)例形式對(duì)比分析了jQuery實(shí)現(xiàn)ajax回調(diào)函數(shù)不帶入?yún)?shù)與帶入?yún)?shù)的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06
基于jQuery的圖片大小自動(dòng)適應(yīng)實(shí)現(xiàn)代碼
這個(gè)和以前弄的圖片遠(yuǎn)處放大有許多相同的地方,比如圖片預(yù)加載、有限容器顯示無限大圖片。2010-11-11
jQuery實(shí)現(xiàn)的支持IE的html滑動(dòng)條
本文給大家分享的是一段使用jQuery實(shí)現(xiàn)支持IE的html滑動(dòng)條代碼,效果非常不錯(cuò),這里推薦給大家,希望大家能夠喜歡。2015-03-03
基于jquery實(shí)現(xiàn)拆分姓名的方法(純JS版)
jquery拆分姓名處理程序如下,純js實(shí)現(xiàn)的,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-05-05
$.each遍歷對(duì)象、數(shù)組的屬性值并進(jìn)行處理
通過$.each,可以遍歷對(duì)象、數(shù)組的屬性值并進(jìn)行處理,下面有個(gè)示例,需要的朋友可以參考下2014-07-07

