博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A JavaScript Image Gallery
阅读量:6914 次
发布时间: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

你可能感兴趣的文章
Loadrunner日志设置与查看
查看>>
美国两大有线电视运营商达成无线服务合作 Verizon的大麻烦来了?
查看>>
Qt之QNetworkInterface
查看>>
深圳卓炎科技的企业网站建设实战经验分享
查看>>
《开源思索集》一开放源码是开源软件吗? - 简书
查看>>
Ubuntu Touch将支持用户数据加密:目前暂无时间表
查看>>
《金蝶ERP—K/3标准财务模拟实训(11.X版)》——导读
查看>>
开发者必备:基于 Linux 生态的十大AI开源框架盘
查看>>
《基于ArcGIS的Python编程秘笈(第2版)》——2.10 更新图层的符号系统
查看>>
SAP的ABAP屏幕程序如何使用Table Control进行数据交互
查看>>
Visual Studio 将集成 Cordova 支持跨平台开发
查看>>
这些方法助你优化 Android 启动速度
查看>>
《简明电路分析》——2.4节单口网络
查看>>
《位置大数据隐私管理》—— 导读
查看>>
如何在 Ubuntu 以及 Debian 中安装 DHCP 服务器
查看>>
《图数据库》——2.1 关系型数据库缺少联系
查看>>
16万Facebook数据告诉你,单身狗更爱养猫
查看>>
《JUnit实战(第2版)》—— 1.6 使用JUnit测试
查看>>
C++程序设计:原理与实践(进阶篇)17.6 Shape
查看>>
《JavaScript设计模式》——1.3 用对象收编变量
查看>>