原生js——与element有关的属性和方法:setAttribute()、getAttribute()、removeAttribute()等
原生js——与element有关的属性和方法
1.属性、属性值:
-
.nodeType:1;
-
.nodeName:元素的标签名;
-
.nodeValue:null;
-
.parentNode:Document || Element;
若要查看 Element 的其他属性值,直接在控制台输出即可; -
.tagName( nodeName ):元素的标签名;
-
.id:元素id名;
-
.className:元素类名;
-
.title:有关元素的附加说明信息;
-
.dir:预言的方向( 可以是ltr,也可以是rtl ),极少用;;
-
.lang:元素内容的语言代码,极少用;
2.方法:
- .getAttribute( attributeName ):获取指定元素的特性,为了避免出现dom事件的bug,最好直接"."来取得对元素特性的使用;
- .setAttribute( newAttributeName,newAttributeValue ):变更指定元素的特性;
- .removeAttribute( attributeName ):彻底移除指定元素的特性;
注:公认的特性(元素本身的特性)既可以通过以上三种方法访问,也可以直接通过.的方式访问;如果是自定义的,非公认的特性,只能通过get、set、remove的方式访问,不可以直接".";
-
.createElement( “tagName” ):创建一个新元素(只能是一个);
// 举例1: var sth = document.createElement("div");
// 举例2: var sth = document.createElement("<div class="select" id="#select"></div>");
-
.getElementById( idName ):通过id获得对元素的引用;
-
.getElementsByClassName( className ):HTML5新增通过id获得对元素的引用;
-
.getElementsByTagName( tagName ):通过标签名获得对元素的引用;
扩展:.namedItem( 索引值 ) :可以通过元素的name特性取得集合中的项,也可以使用" [] "语法来取得对集合中项的引用;注:为防止bug,表单name尽量不要和id相同;
-
.getElementsByName( attributeName ):获取具有指定特性名的所有元素,经常用于表单;
-
.querySelector():HTML5新扩展,获取指定的第一个元素,如果有多个相同的类名的div,则只会获取第一个div;
-
.querySelectorAll():HTML5新扩展,获取指定的所有元素,得到的是一个nodeList集合;
// 注意,如果传入类名,一定要加".",传入id,一定要加"#";这是和其他方法在传值时的不同; var sth = someNode.querySelector(".class"); var sth = someNode.querySelector("#id");
- 原生js——与text有关的属性、方法:appendData()、deleteData()、insertData()、createTextNode()、splitText()……
- 设置 取消input的disabled属性 getattribute(),setAttribute(),removeattribute()用法
- JS-DOM Element方法和属性
- js DOM Element属性和方法整理----转载
- 【转】js中四大命令(getElementById,getElementsByTagName,getAttribute,setAttribute)
- js DOM Element属性和方法整理
- js原生设计模式——8单例模式之简约版属性样式方法库
- html5的classList属性介绍和原生js实现jQuery的addClass,removeClass,hasClass方法
- 四个常用DOM方法(getElementById、getElementByTagName、getAttribute、setAttribute)
- 原生 JS DOM对象 & JQuery对象 常用属性及方法对比(图)
- 有关getAttribute()方法和setAttribute()方法的说明
- JS-DOM Element方法和属性
- js DOM Element属性和方法整理
- document.getElementById("idName")用js方法获取元素,则设定风格属性的方法为
- JS getAttribute和setAttribute(取得和设置属性)的使用介绍
- 原生JS:全局属性、全局方法详解
- 有关getAttribute()方法和setAttribute()方法的说明
- 关于使用js的setAttribute和getAttribute取dom属性在ie ff safri下的问题
- DOM方法(getElementById, getElementsByTagName, getAttribute, setAttribute)
- 有关getAttribute()方法和setAttribute()方法的说明