getAttribute() 與 attr() 的區別


 getAttribute() 和 attr() 都是獲取元素屬性的方法,只是一種是 JS 寫法,一種是 JQ 寫法,但其實它們是有區別的。

主要區別

調用 getAttribute() 的主體必須是元素(Element) 

getAttribute():返回屬性值,是一個文本字符串
getAttributeNode("屬性名"):返回屬性節點,是一個對象
調用 attr() 的主體必須是對象(Object)

JS寫法:getAttribute()

getAttribute() 是元素(Element)下的一種方法,因此想調用這個方法,必須確保它的調用主體是元素,否則會報錯。

正確使用方式:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <div id="test" custom="hello"></div> <script type="text/javascript"> var div = document.getElementById('test'); //獲取的div是[object HTMLDivElement] alert(div.getAttribute('custom')); </script> </body> </html>
  • 錯誤使用方式:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <div id="test" custom="hello"></div> <script type="text/javascript"> var div = $('#test'); //獲取的div是[object Object] alert(div.getAttribute('custom')); </script> </body> </html>
  • 通過 JQ 選擇器獲取 div,此時的 div 是對象(Object)也就無法調用 getAttribute() 方法,瀏覽器(Safari)會報錯如下:

TypeError: div.getAttribute is not a function. (In ‘div.getAttribute(‘custom’)’, ‘div.getAttribute’ is undefined)

JQ寫法:attr()

Get the value of an attribute for the first element in the set of matched elements.

jQuery API Documentation 中對 attr() 方法——准確說是 attr( attributeName ) 方法的描述是“獲取一組相匹配元素中首個元素的屬性值”。 
描述中的“一組元素”應該指的是對象(Object),而不是多個元素組成的集合(HTMLCollection),因為如果方法的執行主體是集合,瀏覽器同樣會報錯:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> </head> <body> <div class="test" custom="hello"></div> <div class="test" custom="hi"></div> <script type="text/javascript"> var div = document.getElementsByClassName('test'); //獲取的div是[object HTMLCollection] alert(div.attr('custom')); </script> </body> </html>
  • 正確使用方式:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> </head> <body> <div class="test" custom="hello"></div> <div class="test" custom="hi"></div> <script type="text/javascript"> var div = $('.test'); //獲取的div是[object object] alert(div.attr('custom')); </script> </body> </html>

1. setAttribute(attributename,attributename) 方法添加指定的屬性,並為其賦指定的值。

屬性可以是自定義的屬性,如果這個指定的屬性已存在,則僅設置/更改值

2. getArribute(attributename);獲取某個屬性的值;返回值為string類型

注:attributename,value都是字符串類型

3. attributes;返回元素屬性的 NamedNodeMap(返回所有屬性的集合,如果通過該方法獲取屬性,obj.attributes['attr'])

注:Internet Explorer 8 以及更早的版本中,attributes 屬性將返回元素所有可能的屬性的集合,即會返回所有隱藏的屬性

attributes中的屬性可以通過數組的方式來獲取對應的屬性值

 

  1.  
    <input type="text" id="txtMsg" myAttr="abc" />
  2.  
    var myAttr = document.getElementById("txtMsg").attributes["myAttr"].value; //通過attributes屬性
  3.  
    var myAttr = document.getElementById("txtMsg").getAttribute("myAttr"); //使用getAttribute方法
  4.  
    document.getElementById("txtMsg").setAttribute("myAttr", "newValue"); //通過setAttribute方法設置屬性的值
  5.  
    var myAttr = document.getElementById("txtMsg").attributes["myAttr"].value; //通過attributes屬性
  6.  
    var myAttr = document.getElementById("txtMsg").getAttribute("myAttr"); //使用getAttribute方法
     

    Python 模塊(Module) 有2種導入方法—— import 和 from … import。盡管它們都是能夠導入模塊,但它們各自導入后的引用、調用方法卻不一樣。

    下面是一個簡單的的模塊 support.py,我們通過它來演示2種導入方法的區別:

    def print_func( par ): print "Hello : ", par return

    使用 import 引入並調用 support 模塊的正確方法:

    #!/usr/bin/python # -*- coding: UTF-8 -*- # 導入模塊 import support # 現在可以調用模塊里包含的函數了 support.print_func("Runoob")
    • 提示:並不能直接使用 print_func() 實現調用,必須將引入的模塊名稱當作一個對象,調用這個模塊對象下的方法 print_func,這時才能實現調用。

    使用 from … import 模塊的正確方法:

    #!/usr/bin/python # -*- coding: UTF-8 -*- # 導入模塊 from support import * # 現在可以調用模塊里包含的函數了 print_func("Runoob")
    • 提示:可以直接使用 print_func() 實現調用。

    筆者建議

    一般來說,推薦使用 import 語句,避免使用 from … import,因為這樣會使你的程序更加易讀,也可以避免名稱沖突。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM