Home > Web Front-end > JS Tutorial > body text

浅谈javascript中的DOM方法_javascript技巧

WBOY
Release: 2016-05-16 15:50:03
Original
1105 people have browsed it

1:获取对象的三种方法

  1)document.getElementById(id);通过对象的属性id来获取;

  2)element.getElementByTagName(tag);通过标签名来获取;

  3)element.getElementByClassName(class);通过属性类名来获取;

2:属性的set和get方法.

  1)object.getAttribute(attribute);获取元素的属性.===>>>HTMLDOM: object.attribute;

  2)object.setAttribute(attribute,value);设置元素的属性.====>>>HTMLDOM:object.attribute=value;

3:有关节点的一些属性

  1)childNodes属性:返回一个数组.

    element.childNodes[0]等价于element.firstChild

    element.childNodes[element.childNodes.length]等价于element.lastChild

  2)nodeType属性:共有12种可能的取值.但其中仅有3种具有使用价值

    元素节点的nodeType=1

    属性节点的nodeType=2

    文本节点的nodeType=3

  3)nodeValue属性:主要功能是改变元素中的文本内容功能类似object.innerHTML

    例如:

这是一个文本节点

    var p=document.getElementById("p");

    p.childNodes[0].nodeValue="改变文本节点的内容";

    这样就改变了p节点中的内容;

    也可以这样实现:p.innerHTML="改变文本节点的内容";

以上所述就是本文的全部内容了,希望大家能够喜欢。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!