javascript DOM编程

javascript DOM编程

1.获取DOM对象

document.getElementById();

document.getElementsByTagName();

document.getElementsByClassName();

document.getElementByName();

document.querySelector(selector);

document.querySelectorAll(selector);

//class操作

document.querySelector(selector).classList

document.querySelector(selector).classList.contains(className)

document.querySelector(selector).classList.remove(className)

document.querySelector(selector).classList.add(className)  //ele.classList.add('a', 'b');

document.querySelector(selector).classList.toggle(className)

2.设置DOM对象属性

DOM.setAttribute("src",source);

DOM.style.color = '#f00';

DOM.style.font = "2em 'Times',serif";

DOM.className = 'name';

DOM.id = 'id';

alert("The font family is " + para.style.fontFamily);

3.获取DOM对象属性值

DOM.getAttribute('src');

DOM.hasAttribute('src');

4.获取DOM节点

DOM.firstChild.nodeValue = text

DOM.firstChild.nodeType = 1 || 2 || 3

元素element 1
属性attr 2
文本text 3
注释comments 8
文档document 9

DOM.nodeName = 'TAGNAME'

DOM.childNodes.length; //childNodes它是标准属性,它返回指定元素的子元素集合,包括HTML节点,所有属性,文本节点,可通过nodeType == 1来判断是否是元素节点

DOM.children;//非标准属性,它返回指定元素的子元素集合,不包括文本节点,相对来说children比childNodes好用,不用过滤什么。需要注意的是children在IE6/7/8中包含注释节点 IE9不包括注释节点

DOM.lastChild.nodeValue

DOM.hasChildNodes()

DOM.previousSibling

DOM.nextSibling

DOM.parentNode

DOM.firstChild //通过firstChild拿回来的元素有可能是文本节点,如果希望拿回来的一定是元素节点,可通过DOM.children[0]的方式获取

DOM.lastChild //类似firstChild

5.创建元素

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

var t = document.createTextNode("I inserted ");

p.appendChild(t);

DOM.insertBefore(newElement,targetElement.nextSibling);

6.插入HTML

DOM.innerHTML = '<p>hello world!</p>';

alert(DOM.innerHTML);

document.write(str);

事件绑定

DOM.onclick = function(){};

DOM.ondblclick = function(){};

DOM.onfocus = function(){};

DOM.onblur = function(){};

DOM.onmouseover = function(){};

DOM.onmouseout = function(){};

DOM onload事件

 1 function addLoadEvent(func) {
 2   var oldonload = window.onload;
 3     if (typeof window.onload != 'function') {
 4       window.onload = func;
 5     } else {
 6     window.onload = function() {
 7       oldonload();
 8       func();
 9     }
10   }
11 }
View Code

7.节流(源自https://keelii.github.io/2016/06/11/javascript-throttle/)

 1 var throttle = function (fn, delay, atleast) {
 2     this.fn      = fn;
 3     this.delay   = delay;
 4     this.atleast = atleast;
 5 
 6     this.init();
 7 };
 8 
 9 throttle.prototype = {
10     init: function () {
11         this.previous = null;
12         this.timer = null;
13     },
14     detect: function () {
15         var now = +new Date();
16 
17         if ( !this.previous ) this.previous = now;
18 
19         // 如果上一次与本次的时间差大于最小触发值,就走最小触发值的逻辑
20         // 否则走 timeout 的逻辑
21         if ( now - this.previous > this.atleast ) {
22             this.atleastCond();
23         } else {
24             this.timeoutCond();
25         }
26     },
27     atleastCond: function () {
28         this.fn();
29         this.previous = +new Date();
30     },
31     timeoutCond: function () {
32         var _this = this;
33 
34         clearTimeout(this.timer);
35         this.timer = setTimeout(function() {
36             _this.fn()
37             _this.previous = null;
38         }, this.delay);
39     }
40 };
41 
42 var th = new throttle(testFn, 200, 1000);
43 window.onresize = function () {
44     th.detect()
45 };

基础知识:

~:表示取反,例如~8,8转换为二进制数,再取反,最终结果是(-8 - 1) = -9。再例如 ~-8,结果为(8 - 1) = 7。

原文地址:https://www.cnblogs.com/moyiqing/p/javascript_DOM.html