测试jsDOM入门笔记

jsDOM入门笔记

  • 按class属性查找标签对象
  • 图片显示初步
  • 页面加载函数。

按class属性查找标签对象

1
2
3
4
5
6
7
8
9
10
11
12
13
function getElementsByClassname(node, classname){
if(node.getElementsByClassName){
return node.getElementsByClassName(classname);
}else{
var results = {};
var elems = node.getElementsByTagName("*");
for(var i = 0; i < elems.length; i++){
if(elems[i].className.indexOf(classname) != -1)
results[results.length] = elems[i];
}
return results;
}
}

图片显示初步

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
//new window
function popUp(winURL){
window.open(winURL, "popup", "width=320,height=320");
return true;
}
//
function picturePreAction(){
//select
if(!document.getElementsByTagName)return false;
if(!document.getElementById)return false;
if(!document.getElementById("popup"))return false;
var popupId = document.getElementById("popup");
var images = popupId.getElementsByTagName("a");

//open a new window;
for(var i=0; i < images.length; i++){
var source = images[i].getAttribute("href");
if(!source) continue;
images[i].onclick = function(){

var source = this.getAttribute("href");
return !popUp(source);

};
}

//show the picture in old page
var links = document.getElementsByTagName("a");
for(var i=0; i < links.length; i++){
var source = links[i].getAttribute("href");
if(!source) continue;

links[i].onmouseover = function(){
var temp = this.getAttribute("href");

var Holder = document.getElementById("pictureHolder");
Holder.setAttribute("src", temp);
};
}
}

页面加载函数。

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