getAttribute()和attr()一点区别
分类:实例代码
关于标题中两个方法的基本用法可以参阅以下两篇文章。
(1).getAttribute()可以参阅getAttribute()一章节。
(2).attr()可以参阅jQuery attr()一章节。
它们两个都是为了获取指定元素的指定属性的值,下面只介绍一下它们之间的一个不同点。
代码实例如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.pipipi.net/" /> <title>前端教程网</title> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script type="text/javascript"> $(document).ready(function (){ var str = ""; str = str+"getAttribute:" + ele.getAttribute("name")+"<br/>"; str = str + "attr:" + $("input").attr("name"); $("#antzone").html(str); }) </script> </head> <body> <input id="ele" type="text" value="前端教程网"/> <div id="antzone"></div> </body> </html>
如果指定的属性不存在,那么返回值一个是null,一个是undefined。
getAttribute()和attr()一点区别,这样的场景在实际项目中还是用的比较多的,关于getAttribute()和attr()一点区别就介绍到这了。
getAttribute()和attr()一点区别属于前端实例代码,有关更多实例代码大家可以查看。