DOM javascript学习 一


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" />
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Image Gallery</title>
<script type="text/javascript" src="c.js"></script>
<link rel="stylesheet" href="styles/layout.css"
type="text/css" media="screen" />
</head>
<body>
<h1>Snapshots</h1>
<ul id="imagegallery">
<li><a href="images/a.jpg" title="a firework display" class="gallerypic">a</a></li>
<li><a href="images/b.jpg" title="a cup of coffee" class="gallerypic">b</a></li>
<li><a href="images/c.jpg" title="a red red rose" class="gallerypic">c</a></li>
<li><a href="images/d.jpg" title="" class="gallerypic">d</a></li>
</ul>
<p id="description">Choose a image.</p>
<img id="placeholder" src="images/placeholder.gif" />
<a href="http://www.baidu.com" class="popUp">popUp</a>
</body>

</html>
</DOCTYPE>


addLoadEvent(prepareLinks);


function prepareLinks(){
//alert("prepareLinks");
if(!document.getElementById) {
return false;
}
if(!document.getElementsByName) {
return false;
}
if(!document.getElementById("imagegallery")){
return false;
}


var gallery = document.getElementById("imagegallery");

var links = gallery.getElementsByTagName("a");
for (var i=0;i<links.length ;i++ ){
if (links[i].getAttribute("class")=="gallerypic"){
//alert("gallerypic"+i);
links[i].onclick = function(){
//为了让showPic()函数所返回的布尔值发挥作用,要加上return ,传递给浏览器
return showPic(this);
//return false; //是否反回false应该由showPic决定而不是由prepareLinks决定
}
//links[i].onkeypress = links[i].onclick; //键盘事件 配合TAB键使用 一般无需添加,onclick可以识别TAB+ENTER
}
}

/*
for (var i=0;i<links.length ;i++ ){
if (links[i].getAttribute("class")=="popUp"){
//alert("to popUp");
links[i].onclick = function(){
popUp(this.getAttribute("href"));return false
}
}
}
*/
//alert("prepareLinks over");
}

function showPic(whichPic){
//salert("showPic");
//检查是否有placeholder 这个ID的元素,如果没有则让html的href跳转
if(!document.getElementById("placeholder")){
alert("there is no element named placeholder");
return true;
}
var source = whichPic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
if(placeholder.nodeName!="IMG"){
return true;
}

placeholder.setAttribute("src",source);

//检查是否有description这个id的元素
if(!document.getElementById("description")){
return false;
}

if(whichPic.getAttribute("title")){
var text = whichPic.getAttribute("title");
} else {
var text = "";
}
var description = document.getElementById("description");

if(description.firstChild.nodeType == 3){
description.firstChild.nodeValue = text;
}

return false;
}

function popUp(winURL){
alert("popUp");
return true;
window.open(winURL,"popup","width=320,height=480");
}


//addLoadEvent! onload函数添加
function addLoadEvent(func){
var oldonload = window.onload;
if(typeof winonload != 'function'){
window.onload = func;
}
else {
window.onload = function(){
oldload();
func();
}
}
}



body{
font-family:"Helvetica","Arial",sans-serif;
color:#333;
background-color:#ccc;
margin:1em 10%;
}

h1{
color:#333;
background-color:transparent;
}

a{
color:#c60;
background-color:transparent;
font-weight:bold;
text-decoration:none;
}

ul{
padding:0;
}

li{
float:left;
padding:1em;
list-style:none;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值