元素

 1 /**
 2 * HTML DOM 元素(节点)*/
 3 
 4 //创建新的HTML元素
 5 var para = document.createElement('p');
 6 var node = document.createTextNode('这是一个新段落');
 7 para.appendChild(node);
 8 
 9 var box = document.getElementById('box');
10 box.appendChild(para);
11 
12 //删除已有的HTML元素
13 box.removeChild(para);

 

 

对象

 1 /***
 2  * 对象:带有属性和方法的特殊数据类型
 3  * 内建对象:String、Date、Array
 4  *
 5  * 创建对象的方法:
 6  * 1.创建直接的实例或literals模式
 7  * 2.使用对象构造器
 8  *
 9  * JavaScript 基于 prototype,而不是基于类的。
10  */
11 
12 //创建直接的实例
13 var person = new Object();
14 person.firstName = 'Liu';
15 person.lastName = 'QiuChen';
16 person.age = 100;
17 person.favorite = 'animals';
18 
19 console.log(person);
20 
21 //literals模式
22 var rabbit = {
23     firstName: 'Er',
24     lastName: 'Bao',
25     age: 18,
26     favorite: 'lqc'
27 };
28 
29 console.log(rabbit);
30 
31 //使用对象构造器
32 function Obj(firstName, lastName, age, favorite) {
33     this.firstName = firstName;
34     this.lastName = lastName;
35     this.age =age;
36     this.favorite = favorite;
37 
38     this.f = function () {
39         console.log('这是构造函数里的方法');
40     };
41 }
42 
43 
44 var obj1 = new Obj('Hua', 'Dou', 50, 'family');
45 console.log(obj1);
46 
47 //遍历对象
48 for(var variable in obj1) {
49     console.log(variable + ": " + obj1[variable]);
50 }

 

posted on 2016-05-04 23:06  Asina  阅读(188)  评论(0编辑  收藏  举报