博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A JavaScript Image Gallery
阅读量:6913 次
发布时间:2019-06-27

本文共 3458 字,大约阅读时间需要 11 分钟。

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 get all the children of the body element.
    var body_element = document.getElementsByTagName("body")[0];
To access the children of the body element, you just need to use :
    body_element.childNodes; 
you may write a function to find out how many elements the body element contains :
function countBodyChildren() {       var body_element = document.getElementsByTagName("body")[0] ;       alert( body_element.childNodes.length );  }
If you want this function to be excuted when the page loads, you can use the onload event handler
to do this. 
        window.onload = countBodyChildren ;
When the document loads, the countBodyChildren function will be invoked.
 

 
nodeType property :
This will tell us exactly what kind of node we're dealing with. 
The nodeType property is called with the following syntax : 
node.nodeType
instead of returning a string like "element" or "attribute", it returns a number.
There are 12 possible values for nodeType, but only 3 of them are going to be of much practical use:
  •         Element nodes have a nodeType value of 1
  •         Attribute nodes have a nodeType value of 2
  •         Text nodes have a nodeType value of 3
 

 
nodeValue property :
If you want to change the value of a text node, there is a DOM property called nodeValue that can be 
used to get (and set) the value of a node :
node.nodeValue
 
firstChild and lastChild property :
node.firstChild  ==  node.childNodes[0]
node.lastChild   ==  node.childNodes[node.childNodes.length-1]
 

This image gallery projects are as follws :
/***      index.html      ***/
    
Image Gallery

Snapshiots

my image gallery

Choose an image

View Code

/***      showPic.js      ***/

/** * Created by Administrator on 9/9/2015. *//*    you can use this function to count how many children nodes the body element contains */function countBodyChildren() {    var body_element = document.getElementsByTagName("body")[0];    alert(body_element.nodeType);    alert( body_element.childNodes.length );}function showPic(whicPic) {    var source = whicPic.getAttribute("href");    var text = whicPic.getAttribute("title");    var placeholder = document.getElementById("placeholder");    var img = placeholder.getElementsByTagName("img")[0];    img.setAttribute("src", source);    var description = document.getElementById("description");    var desc_p = description.getElementsByTagName("p")[0];    desc_p.firstChild.nodeValue = text;}
View Code

/***      layout.css      ***/

body{
font-family: "Helvetica", "Arial", serif; color: #333; background-color: #ccc; margin: 1em 10%;}h1{
color: #333; /*background-color: #777;*/}a{
color: #c60; background-color: transparent; font-weight: bold; text-decoration: none;}ul{
padding: 0;}li{
float: left; padding: 1em; list-style: none;}img {
display: block; clear: both;}
View Code

The structure are like the pic shows below :

 
 

转载于:https://www.cnblogs.com/beyond-Acm/p/4795682.html

你可能感兴趣的文章
pdftk的使用介绍
查看>>
hibernate _关联级别策略介绍
查看>>
Word如何快速绘制你需要的作文稿纸
查看>>
币圈的黄埔军校,徐明星的傲慢与偏见
查看>>
全球直播的罗胖跨年演讲背后技术支撑故事——罗辑思维首席架构师方圆访谈...
查看>>
万能的淘宝再现实力“魔术手”,沙县小吃摇身变成“萌系治愈所”
查看>>
勒索病毒致世界天翻地覆 360安全卫士离线救灾版扭转乾坤
查看>>
排插新国标将实施,传统排插企业满脸“尴尬”?
查看>>
36岁辞职创业,5名员工,如今年销售额50亿!
查看>>
势不可挡!荣耀双11再夺销量和销售额的双料冠军
查看>>
华为Mate 10 Pro:4800秒从0%至100%的神奇电池
查看>>
PCIe NVMe SSD准备”蚕食”企业级存储系统了
查看>>
“引江济淮”工程全线开工 将建全球单跨最大渡槽
查看>>
青瓦台:特朗普未曾提及韩方协防分担费具体数额
查看>>
福岛第一核电站将撤除屋顶钢筋 为取出燃料做准备
查看>>
贵阳铁路警方早谋划 多举措备战春运保安全
查看>>
“全国百名杰出新型职业农民”刘良军:从医官到村官
查看>>
深商全球大会:百果园获评“深圳老字号”
查看>>
政协委员建言川港两地深化合作 聚焦青年人才交流
查看>>
“脱欧”困境难解 英欧关系或面临十字路口
查看>>