多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > web前端 > htmlcss > [JS]jQuery中attr和prop方法的區(qū)別

[JS]jQuery中attr和prop方法的區(qū)別

來源:程序員人生   發(fā)布時間:2015-02-24 21:30:27 閱讀次數(shù):4666次

相比attr,prop是1.6.1才新出來的,二者從中文意思理解,都是獲得/設(shè)置屬性的方法(attributes和properties)。只是,window或document中使用.attr()方法在jQuery1.6之前不能正常運(yùn)行,由于window和document中不能有attributes。prop應(yīng)運(yùn)而生了。

之前看網(wǎng)上對照二者的文章,更是列出1個表來辨別甚么標(biāo)簽下使用prop,甚么標(biāo)簽下使用attr,諒解我是怠惰的人,最懼怕要背的東西,所以只有自己想一想辦法了。

既然我們想知道他們兩的區(qū)分,最好就看看他們的源代碼,不要被代碼長度所嚇到,我們只看關(guān)鍵的幾句:

attr方法代碼(jQuery版本1.8.3)

  • attr: function( elem, name, value, pass ) {
  • var ret, hooks, notxml,
  • nType = elem.nodeType;
  • // don't get/set attributes on text, comment and attribute nodes
  • if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
  • return;
  • }
  • if ( pass && jQuery.isFunction( jQuery.fn[ name ] ) ) {
  • return jQuery( elem )[ name ]( value );
  • }
  • // Fallback to prop when attributes are not supported
  • if ( typeof elem.getAttribute === "undefined" ) {
  • return jQuery.prop( elem, name, value );
  • }
  • notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
  • // All attributes are lowercase
  • // Grab necessary hook if one is defined
  • if ( notxml ) {
  • name = name.toLowerCase();
  • hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook );
  • }
  • if ( value !== undefined ) {
  • if ( value === null ) {
  • jQuery.removeAttr( elem, name );
  • return;
  • } else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) {
  • return ret;
  • } else {
  • elem.setAttribute( name, value + "" );
  • return value;
  • }
  • } else if ( hooks && "get" in hooks && notxml && (ret = hooks.get( elem, name )) !== null ) {
  • return ret;
  • } else {
  • ret = elem.getAttribute( name );
  • // Non-existent attributes return null, we normalize to undefined
  • return ret === null ?
  • undefined :
  • ret;
  • }
  • }

prop方法代碼(jQuery版本1.8.3)

  • prop: function( elem, name, value ) {
  • var ret, hooks, notxml,
  • nType = elem.nodeType;
  • // don't get/set properties on text, comment and attribute nodes
  • if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
  • return;
  • }
  • notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
  • if ( notxml ) {
  • // Fix name and attach hooks
  • name = jQuery.propFix[ name ] || name;
  • hooks = jQuery.propHooks[ name ];
  • }
  • if ( value !== undefined ) {
  • if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) {
  • return ret;
  • } else {
  • return ( elem[ name ] = value );
  • }
  • } else {
  • if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) {
  • return ret;
  • } else {
  • return elem[ name ];
  • }
  • }
  • }

attr方法里面,最關(guān)鍵的兩行代碼,elem.setAttribute( name, value + “” )和ret = elem.getAttribute( name ),很明顯的看出來,使用的DOM的API setAttribute和getAttribute方法操作的屬性元素節(jié)點(diǎn)
而prop方法里面,最關(guān)鍵的兩行代碼,return ( elem[ name ] = value )和return elem[ name ],你可以理解成這樣document.getElementById(el)[name] = value,這是轉(zhuǎn)化成JS對象的1個屬性。

既然明白了原理是這樣,我們來看看1個例子:

  • <input type= 生活不易,碼農(nóng)辛苦
    如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈
    程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 岛国视频在线播放 | 欧美视频xxx | 性做久久久久久久久 | xxxx tube hd人妖| 最近的中文字幕视频大全高清 | 亚洲国产成人在线观看 | 国产亚洲一欧美一区二区三区 | 国产亚洲美女精品久久久久 | 国产毛片在线视频 | 网友自拍视频悠悠在线 | 精品久久中文网址 | 亚洲精品影院 | 成人高清毛片a | youjizz久久 | 日韩欧美国内 | 黄色毛片视频网站 | 午色 | a级片中文字幕 | 黄色网址网站在线观看 | 国产香蕉一区二区精品视频 | 牛仔裤美女国产精品毛片 | 亚洲 春色 另类小说 | 色综合久久久高清综合久久久 | 午夜影院在线免费观看 | 欧美高清视频一区 | 激情久久久久久久久久 | 嫩草影院在线观看精品视频 | 一级做a爰片欧美一区 | 亚洲视频精品 | 欧美xxxxxxxxxxxxx 欧美xxxxxxxxxx黑人 | 最近免费中文字幕mv | 久久99国产精一区二区三区! | 亚洲另类春色小说 | 波多野结衣视频在线播放 | 亚洲 欧美 在线观看 | 日韩综合久久 | 国产精品成人免费 | 成人中文字幕在线观看 | 亚洲精品在线播放视频 | 最新免费黄色网址 | 欧美国产精品亚洲精品第一区 |