nodeValue、firstChild和lastChild属性
nodeValue属性
如果想改变一个文本节点的值,那就使用DOM提供的nodeValue属性,他用来得到(和设置)一个节点的值:
node.nodeValue
但是有个细节必须注意:在用nodeValue属性获取description对象的值时,得到的并不是包含在这个段落里的文本。
可以用下面这条alert语句来验证这一点:
alert(description.nodeValue);
html代码如下
<html>
<body>
<p id = "description">ssssss</p>
</body>
</html>
这个调用将返回一个null值。<p>元素本身的nodeValue属性是一个空值,而你真正需要的是<p>元素所包含的文本的值。
包含在<p>元素里的文本是另一种节点,它是<p>元素的第一个子节点。因此,你想要得到的其实是它的第一个子节点的nodeValue属性值。
下面这个条alert语句可以显示你想要的内容:
alert(description.childNodes[0].nodeValue);
这个值来自childNodes数组的第一个(下标是0)元素。
firstChild和lastChild属性
数组元素childNodes[0]有个更直观易读的同义词。无论何时何地,只要需要访问childNodes数组的第一个元素,都可以把它写成firstChild:
node.firstChild
这种写法与下面的写法完全等价:
node.childNodes[0]
DOM还提供了一个与之对应的lastChild属性:
node.lastChild
这代表着childNodes数组的最后一个元素。
nodeValue、firstChild和lastChild属性的更多相关文章
- JavaScript之firstChild属性、lastChild属性、nodeValue属性学习
1.数组元素childNodes[0]有更直观易读的优点,这边在介绍一个有同样功能的属性,且更加语义化-------->firstChild属性 假设我们需要目标元素节点下的所有子元素中的第一个 ...
- 选择器:first-child与:last-child失效的解决方法
作为还在努力练习的代码小白来说,有时类名或者ID名太多很容易就会搞混,为此,在练习中会想着借用多样的选择器来设置而不是每一个标签都设一个类名(Id名),在此次练习中使用选择器:first-child与 ...
- CSS nth-child、first-child、last-child、nth-of-type、first-of-type和last-of-type选择器使用
以下示例主要讲解nth-child.first-child.last-child.nth-of-type.first-of-type和last-of-type使用. 示例代码: <!DOCTYP ...
- CSS3中first-child、last-child、nth-child、nth-last-child
1.单独指定第一个子元素.最后一个子元素的样式 <style type="text/css"> li:first-child{ background:yellow; } ...
- CSS选取指定位置标签first-child、last-child、nth-child
1.first-child 选择列表中的第一个标签. 2.last-child 选择列表中的最后一个标签 3.nth-child(n) 选择列表中的第n个标签 4.nth-child(2n) 选择列表 ...
- first-child和last-child选择器 nth-child(n)第几个元素 nth-last-child(n)倒数第几个元素
:first-child 和 :last-child 分别表示父元素中第一个 或者 最后一个 子元素设置样式,如上图
- 选择器的使用(first-child和last-child选择器)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- first-child、last-child误解
MDN解释兄弟元素中的第一个元素 然后今天写的时候这样想出现了问题 并没有加上边框 W3C解释 尝试去掉h3,发现span加上了边框 E:first-child含义 父元素中第一个元素且第一个元素是E ...
- jQuery中的子(后代)元素过滤选择器(四、六):nth-child()、first-child、last-child、only-child
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
随机推荐
- win7 Python 环境 准备 配置
包括Python,eclipse,jdk,pydev,pip,setuptools,beautifulsoup,pyyaml,nltk,mysqldb的下载安装配置. **************** ...
- 【分享】Python学习资源大合集
地址:http://www.hejizhan.com/html/xueke/520/x520_03.html Python安装软件合集(Windows)(78) Python教程——游戏编程(13) ...
- ASP.NET MVC 缓存扩展 - Donut Caching
项目介绍 ASP.NET MVC Extensible Donut Caching brings donut caching to ASP.NET MVC 3 and later. The code ...
- SpecFlow使用入门之C# BDD
SpecFlow使用入门 http://www.specflow.org/ SpecFlow是一个BDD工具,在这里对BDD不多赘述,你可以阅读一下微软2010年十二月的一篇文章,此外如果你想要更多了 ...
- 最短路径 dijkstra
最短路径 dijkstra #include <stdio.h> #include <string.h> #include <limits.h> #define M ...
- C#打包应用程序
摘要:本文介绍在C#中手把手教你用C#打包应用程序(安装程序卸载程序) 1:新建安装部署项目 打开VS,点击新建项目,选择:其他项目类型->安装与部署->安装向导(安装项目也一样),然后点 ...
- java使用javamail读取邮箱(收件箱为例)
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- poj2187(未完、有错)
凸包求直径(socalled..) 采用Graham+Rotating_Calipers,Graham复杂度nlogn,RC算法复杂度n,所以时间复杂度不会很高. 学习RC算法,可到http://cg ...
- HDU1423:Greatest Common Increasing Subsequence(LICS)
Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...
- [转]Bypassing iOS security
src: http://blog.thireus.com/tag/kernelcache Before going further it is important to enumerate some ...