-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dom scripting.html
239 lines (204 loc) · 6.12 KB
/
Dom scripting.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html>
<head>
<title>Dom Scripting</title>
<link rel="stylesheet" type="text/css" href="./CSS/home.css">
<style></style>
</head>
<body>
<H1>Dom Scripting</H1>
<div id="container"></div>
<div id="footer">
<h3> Document Information </h3>
<ul>
<li>
Date: 2015/03/17
</li>
<li>
Reference:
<ul>
<li><a target="_blank" href="http://www.github.com">github</a></li>
<li><a target="_blank" href="http://www.google.com">google</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>
<script src="./LIB/jquery/jquery-2.1.3.js"></script>
<script src="./LIB/google-code-prettify/src/run_prettify.js"></script>
<script src="./LIB/home.js"></script>
<script type="text/javascript">
/* Document Object Model
- elements node
- attributes node
- text node
*/
/*
getElementsByClassName is only support for HTML5 DOM.
*/
(function(){
try{
var getElementsByClassName = function(node, className){
if(node.getElementsByClassName){
return node.getElementsByClassName(className);
} else {
var results = [];
var elems = node.getElementsByTagName("*");
for(var i = 0, j = elems.length; i < j; i++){
if(elems[i].className.indexOf(classname) !== -1){
results.push(elems[i]);
}
}
return results;
}
}
var oContainer = document.getElementById("container");
var oCodes = getElementsByClassName(oContainer, "codePanel");
console.log(oCodes.length);
}catch(e){
console.error(e.message);
}
}());
/*
getAttribute/ setAttribute is only works for element node
*/
(function(){
try{
var oTitles = document.getElementsByTagName("H1");
for(var i = 0, j = oTitles.length; i < j; i++){
var title_text = oTitles[i].getAttribute("title");
if(title_text){
console.log(title_text);
oTitles[i].setAttribute("title", title_text + " --Updated");
//Same effect as above: oTitles[i].title = title_text + " --Updated";
}
}
}catch(e){
console.error(e.message);
}
}());
/*
return false to avoid default behavior
<a href="http://www.example.com" onclick="return false;">Click me</a>
*/
/*
childNodes, firstChild, lastChild, nodeType, nodeValue
nodeType
- element 1
- attribute 2
- text 3
*/
(function(){
try{
var body_element = document.getElementsByTagName("body")[0];
var aChildNodes = body_element.childNodes;
console.log(aChildNodes.length);
console.log(aChildNodes.firstChild.nodeType);
console.log(aChildNodes.lastChild.nodeValue);
}catch(e){
console.error(e.message);
}
}());
/*
Javascript pseudo-protocol
<a href="javascript:doSomethingFn('www.baidu.com')">Example</a>
Inline event handlers
<a href="www.baidu.com" onClick="doSomethingFn('www.baidu.com')">Example</a>
Best practice
<a href="www.baidu.com" onClick="doSomethingFn(this.href); return false">Example</a>
*/
/*
Performance consideration
*/
/*
Minimizing DOM access and markup
*/
(function(){
try{
//Assign the original result to a variable and reuse the same result in the loop.
var links = document.getElementsByTagName("a");
if(links.length > 0){
for(var i = 0, j = links.length; i < j;i++){
//do something to each link
}
}
}catch(e){
console.error(e.message);
}
}());
/*
Assembling and placing scripts
- combine all the js file into a single file
- including all your js file at the end of the document.
*/
/*
Minification
- Douglas Crockford's JSMin (http://www.crockford.com/javascript/jsmin.html)
- Yahoo!'s YUI Compressor (http://developer.yahoo.com/yui/compressor/)
- Google's Closure Compiler (http://closure-compiler.appspot.com/home)
*/
(function(){
try{
console.log("Code snippet2");
}catch(e){
console.error(e.message);
}
}());
/* Title 2 */
(function(){
try{
console.log("Code snippet2");
}catch(e){
console.error(e.message);
}
}());
/* Title 2 */
(function(){
try{
console.log("Code snippet2");
}catch(e){
console.error(e.message);
}
}());
/* Title 2 */
(function(){
try{
console.log("Code snippet2");
}catch(e){
console.error(e.message);
}
}());
/* Title 2 */
(function(){
try{
console.log("Code snippet2");
}catch(e){
console.error(e.message);
}
}());
/* Title 2 */
(function(){
try{
console.log("Code snippet2");
}catch(e){
console.error(e.message);
}
}());
/* Title 2 */
(function(){
try{
console.log("Code snippet2");
}catch(e){
console.error(e.message);
}
}());
/* Title 2 */
(function(){
try{
console.log("Code snippet2");
}catch(e){
console.error(e.message);
}
}());
</script>