Javascript Image Gallery 修改

文件目录:

其中,所需的只有 showPic2.js & gallery2.html & gallery2.css & images

gallery2.html :

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"/>
	<title>Image Gallery</title>
	<link rel="stylesheet" type="text/css" href="gallery2.css" />
</head>
<body>
	<h1> Snapshots </h1>
	<ul id="imagegallery">
		<li>
			<a href="images/fireworks.jpg" title="A fireworks display"> Fireworks </a>
		</li>
		<li>
			<a href="images/coffee.jpg" title="A cup of coffee"> Coffee </a>
		</li>
		<li>
			<a href="images/rose.gif" title="A rose"> Rose </a>
		</li>
		<li>
			<a href="images/bigben.jpg" title="The famous clock"> Big Ben </a>
		</li>
	</ul>
	<img id="placeholder" src="images/placeholder.jpg" alt="my image gallery" />
	<p id="description"> Choose an image. </p>
	<script type="text/javascript" src="showPic2.js"></script>

	<!--
	Some Notes 

	[1] If Javascript support is disabled in consumer‘s browser, we don‘t worry about consumer cann‘t see pictures, because we use a real link in "href" in order to make browser work properly.

	[2] We separate document‘s structure and document‘s actions , and much more important thing is we use id "imagegallery" of tag <ul> to make Javascript code be associated with htmldocument‘s tags.

	-->

</body>
</html>

  

gallery2.css :

body{
	font-family: "Helvetica","Arial","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;
	cursor: pointer;
}
ul{
	padding: 0;
}
li{
	float: left;
	padding: 1em;
	list-style: none;
}
img{
	display: block;
	clear: both;
}

  

showPic2.js :

window.onload = prepareGallery;
function prepareGallery()
{
	if(!document.getElementsByTagName)
		return false;
	if(!document.getElementById)
		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++)
	{
		links[i].onclick = function()
		{
			return showPic(this) ? false : true;
		}
	}
}

function showPic(whichpic)
{
	if(!document.getElementById("placeholder")) return false;
	var source = whichpic.getAttribute("href");
	var placeholder = document.getElementById("placeholder");
	if(placeholder.nodeName != "IMG") return false;
	placeholder.setAttribute("src",source);
	if(document.getElementById("description"))
	{
		var text = whichpic.getAttribute("title") ? whichpic.getAttribute("title") : "";
		var description = document.getElementById("description");
		if(description.childNodes[0].nodeType == 3)
		{
			description.childNodes[0].nodeValue = text;
		}
	}
	return true;
}

  

showPic.js 是关于showPic2.js中代码的解释和一些注意事项:

window.onload = prepareGallery;
function prepareGallery()
{
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	if(!document.getElementById("imagegallery")) return false;
	/*
	We use 3 if statements to check the current browser whether surpports & understands those functions of docunment or not.
	The last if statement is used to check that whether it exists an id "imagegallery" in the associated html file.
	A small tip: if you like putting return statement in a new line, you had better put return statement into a {}.
	*/
	var gallery = document.getElementById("imagegallery");
	var links = gallery.getElementsByTagName("a");

	for(var i = 0; i < links.length;i++)  // traverse all the links of tags <a> in a for loop
	{
		links[i].onclick = function()  // bind every link to a function showPic()
		{
			return showPic(this) ? flase : true;
			// to solve the problem that it will jump to another web page when you click links if it doesn‘t return false, and current link will pass to the function showPic() as a parameter
			// and this method of return can solove the problem that you can not open any picture when you delete the "placeholder"
			// if showPic(this) return true(mean that image switches successfully in the showPic()), then prepareGallery() will return false to cancel the default behavior of "onclick"; if showPic() return false, the prepareGallery() will return true to make images can also be opened
		}
	}
}

function showPic(whichpic)
{
	if(!document.getElementById("placeholder")) return false;
	var source = whichpic.getAttribute("href");
	var placeholder = document.getElementById("placeholder");
	if(!placeholder.nodeName != "IMG") return false;
	// Be attention! nodeName property always return a value of a Capture, even it is a lowercase in html
	placeholder.setAttribute("src",source);

	if(document.getElementById("description"))
	{
		var text = whichpic.getAttribute("title");
		var description = document.getElementById("description");
		if(description.childNodes[0].nodeType == 3)
		{
			description.childNodes[0].nodeValue = text;
		}
	}
	return true;
}

/*
Some Notes:

structed programming : three is a tenet in this theory -- a function can only have one entrance and one exit.
However, in function prepareGallery(), we have three exits!
In my own opinion, it can be accepted if a function has multiple exits, as long as these exits concentrate in the beginning of a funcition.

onclick  is very smart : we don‘t worry what will happen if costumer use keyboard to click links,
because it will trigger the onclick event when you use a tab key to move to a link after pressing the enter key in almost every browser,
so, there is no need for using "onkeypress" event! And "onkeypress" event is not an impeccable function

In this javascript code, I only use four functions of DOM:
[1] getElementById
[2] getElementsByTagName
[3] getAttribute
[4] setAttribute
and we can use HTML-DOM to simplify codes:
document.getElementsByTagName("form") <=> document.forms (HTML-DOM provides object "forms")
element.getAttribute("src") <=> element.src (HTML-DOM provides many propertiees describing html elements: src, href , title and so on)
However, I think DOM Core is easier to use.
*/

  

网页效果:

点击后:

这里完善了图片库中的一些内容。

时间: 2024-10-15 18:15:56

Javascript Image Gallery 修改的相关文章

JavaScript函数内部修改全局变量的问题【一道面试题】

JavaScript函数内部修改全局变量的问题 今天 10:44梵天莲华 | 浏览 23 次 Javascript编程语言函数 修改标签 代码如下,为什么加了 function a(){};这个函数,就不能改变全局变量a的值了? var a = 1; function b(){ a = 2; console.log(a); //有函数,a不变:没函数,a变2 function a(){}; } b();//输出2 console.log(a);//输出1 今天 11:07 提问者采纳 因为A.

javascript原型的修改与重写(覆盖)差别

每个JavaScript函数都有prototype属性(javascript对象没有这个属性),这个属性引用了一个对象,这个对象就是原型对象.javascript允许我们修改这个原型对象.修改有2种方式: 方式1:在原有的原型对象上增加属性或者方法 function Person() { } Person.prototype.add = function(){ alert(this.name); }; Person.prototype.name = "aty"; var p1 = ne

JavaScript之表格修改

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

A JavaScript Image Gallery

childNodes property: The childNodes property is a way of getting information about the children of any element in a document's node tree. It returns an array containing all the children of an element node : element.childNodes; Lets say you wanted to

javascript读取和修改原型特别需要注意的事儿,因为原型的读写不具有对等性

对于从原型对象继承而来的成员,其读和写具有内在的不对等性.比如有一个对象A,假设它的原型对象是B,B的原型对象是null.如果我们需要读取A对象的name属性值,那么JS会优先在A中查找,如果找到了name属性那么就返回:如果A中没有name属性,那么就到原型B中查找name,如果找到了就返回:如果原型B中也没有找到,由于此时已经到了原型链的最顶端,还是没有找到name,就直接返回undefined.在写的情况下,执行A.name="aty",如果A中有name属性,那么会修改name

Javascript Images Gallery

文件目录: gallery.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Image Gallery</title> <style type="text/css"> body{ font-family: "Helvetica","Arial","se

EasyUI+JavaScript 添加和修改弹出框按钮

写这篇文章只是想吐槽一下在做三个弹出框按钮的心酸史,为什么只写两个呢?因为之前看到过别人写了一个删除的,所以我只写剩下的两个吧!现在做的系统总是涉及到增删改的总会上面三个好看的按钮,下面一个好看的DataGrid. 添加和修改弹出框: 首先要加载我们做好的添加和编辑弹出框. <span style="font-size:14px;"><div id="addTemplate"> @* 加载添加流程对话框 *@ @{Html.RenderPar

javascript和jquery修改a标签的href属性

javascript: 代码如下: document.getElementById("myId").setAttribute("href","www.xxx.com"); document.getElementById("myId").href = "www.xxx.com"; jquery: 代码如下: $("#myId").attr("href","ww

javascript:cssText 修改元素的style样式 - &nbsp; &nbsp; - innerHTML 修改元素的内容!

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> div { width:100px; height:100px; border:1px solid #333; } </sty