JavaScript 客户端JavaScript之 脚本化文档
客户端JavaScript的存在把静态HTML转变为交互式的Web应用程序,脚本化Web页面的内容正是JavaScript存在的理由。
<body>
<input type="button" value="打开一个新窗口" id="bt1"/>
</body> window.onload = function () {
var btn = document.getElementById("bt1");
btn.onclick = function () {
//var o = window.open("test.aspx");
var o = window.open();
var doc = o.document;
doc.open();
doc.write("this ");//会覆盖原有页面的内容
doc.write(" is");
doc.write(" a");
doc.write(" test");
doc.close(); }
}
console.log("document.bgColor:" + document.bgColor + "\ndocument.cookie:" + document.cookie + "\n");
console.log("document.domain:" + document.domain + "\ndocument.lastModified:" + document.lastModified + "\n");
console.log("document.location:" + document.location + "\ndocument.referrer:" + document.referrer + "\n");
console.log("document.URL:" + document.URL);
下面为输出:
document.bgColor:
document.cookie:
document.domain:localhost
document.lastModified:03/25/2015 21:36:48
document.location:http://localhost:1344/default.aspx
document.referrer:
document.URL:http://localhost:1344/default.aspx"
<form name="form1">
<input type="button" name="fbtn" value="打开一个新窗口" id="bt1"/>
</form>
document.form1.name2
window.onload = function () {
document.form1.fbtn.onclick = function () {
//var o = window.open("test.aspx");
var o = window.open()
var doc = o.document;
doc.open();
doc.write("this ");
doc.write(" is");
doc.write(" a");
doc.write(" test");
doc.close();
}
}
HTML DOM 树

通过可编程的对象模型,JavaScript 获得了足够的能力来创建动态的 HTML。
- JavaScript 能够改变页面中的所有 HTML 元素
- JavaScript 能够改变页面中的所有 HTML 属性
- JavaScript 能够改变页面中的所有 CSS 样式
- JavaScript 能够对页面中的所有事件做出反应
节点父、子和同胞
节点树中的节点彼此拥有层级关系。
父(parent)、子(child)和同胞(sibling)等术语用于描述这些关系。父节点拥有子节点。同级的子节点被称为同胞(兄弟或姐妹)。
- 在节点树中,顶端节点被称为根(root)
- 每个节点都有父节点、除了根(它没有父节点)
- 一个节点可拥有任意数量的子
- 同胞是拥有相同父节点的节点
nodeName、nodeValue 以及 nodeType 包含有关于节点的信息。
nodeName 属性含有某个节点的名称。
- 元素节点的 nodeName 是标签名称
- 属性节点的 nodeName 是属性名称
- 文本节点的 nodeName 永远是 #text
- 文档节点的 nodeName 永远是 #document
注释: nodeName 所包含的 XML 元素的标签名称永远是大写的
nodeValue
对于文本节点,nodeValue 属性包含文本。
对于属性节点,nodeValue 属性包含属性值。
nodeValue 属性对于文档节点和元素节点是不可用的。
nodeType
nodeType 属性可返回节点的类型。
最重要的节点类型是:
| 元素类型 | nodeTyep的值 |
接口
|
|---|---|---|
| 元素element | 1 |
Element
|
| 属性attr | 2 |
Attr
|
| 文本text | 3 |
Text
|
| 注释comments | 8 |
Comment
|
| 文档document | 9 |
Document
|
|
DocumentFragment
|
11
|
DocumentFragment
|
|
特性名
|
版本 |
描述
|
|
|
HTML
|
1.0
|
1级Core和HTML接口
|
|
|
XML
|
1.0
|
1级Core和XML接口
|
|
|
Core
|
2.0
|
2级Core接口
|
|
|
HTML
|
2.0
|
2级HTML接口
|
|
|
XML
|
2.0
|
2级XML接口
|
|
|
Views
|
2.0
|
AbstractView接口
|
|
|
StyleSheets
|
2.0
|
通用样式表遍历
|
|
|
CSS
|
2.0
|
CSS样式
|
|
|
CSS2
|
2.0
|
CSS2Properties接口
|
|
|
Events
|
2.0
|
事件处理基础结构
|
|
|
UIEvents
|
2.0
|
用户接口事件(加上Events和Views)
|
|
|
MouseEvents
|
2.0
|
Mouse事件
|
|
|
HTMLEvents
|
2.0
|
HTML事件
|
|
|
CSS3
|
|
CSS3Properties接口
|
|
<form name="form1">
<input type="button" name="fbtn" value="创建一个节点" id="bt1" />
</form>
window.onload = function () {
document.form1.fbtn.onclick = function () {
var t = document.createTextNode("文本节点");
document.form1.appendChild(t);
}
}
<form name="form1">
<input type="button" name="fbtn" value="查询选定的文本" id="bt1" />
这是一个可选的文本
</form> window.onload = function () {
document.form1.fbtn.onclick = function () {
var s="";
if (window.getSelection)
{
s = window.getSelection();
}
else if (window.Selection)
{
s = window.Selection;
}
else if (document.getSelection)
{
s = document.getSelection();
}
alert(s);
}
}
一些 DOM 对象方法
这里提供一些您将在本教程中学到的常用方法:
| 方法 | 描述 |
|---|---|
| getElementById() | 返回带有指定 ID 的元素。 |
| getElementsByTagName() | 返回包含带有指定标签名称的所有元素的节点列表(集合/节点数组)。 |
| getElementsByClassName() | 返回包含带有指定类名的所有元素的节点列表。 |
| appendChild() | 把新的子节点添加到指定节点。 |
| removeChild() | 删除子节点。 |
| replaceChild() | 替换子节点。 |
| insertBefore() | 在指定的子节点前面插入新的子节点。 |
| createAttribute() | 创建属性节点。 |
| createElement() | 创建元素节点。 |
| createTextNode() | 创建文本节点。 |
| getAttribute() | 返回指定的属性值。 |
| setAttribute() | 把指定属性设置或修改为指定的值。 |
JavaScript 客户端JavaScript之 脚本化文档的更多相关文章
- Javascript学习8 - 脚本化文档(Document对象)
原文:Javascript学习8 - 脚本化文档(Document对象) 每个Web浏览器窗口(或帧)显示一个HTML文档,表示这个窗口的Window对象有一个document属性,它引用了一个Doc ...
- JavaScript权威指南--脚本化文档
知识要点 脚本化web页面内容是javascript的核心目标. 第13章和14章解释了每一个web浏览器窗口.标签也和框架由一个window对象所示.每个window对象有一个document对象, ...
- JavaScript权威设计--JavaScript脚本化文档Document与CSS(简要学习笔记十五)
1.Document与Element和TEXT是Node的子类. Document:树形的根部节点 Element:HTML元素的节点 TEXT:文本节点 >>HtmlElement与 ...
- javascript脚本化文档
1.getElememtById /** * 获取指定id的的元素数组 */ function getElements(/*ids...*/) { var elements = {}; for(var ...
- JavaScript 客户端JavaScript之脚本化HTTP(通过XMLHttpRequest)
XMLHttpRequest对象的设计目的是为了处理由普通文本或XML组成的响应:但是,一个响应也可能是另外一种类型,如果用户代理(UA)支持这种内容类型的话. 大多数浏览的客户端JavaScri ...
- JavaScript 客户端JavaScript之 脚本化浏览器窗口
1.计时器 客户端Javascript以全局函数setTimeOut().clearTimeOut().setInterval().clearInterval()提供这一功能. 前者是从运行的那一 ...
- JavaScript学习笔记7 之DOM文档对象模型
一.什么是DOMDocument Object Model 文档 -------对象 ----模型-------缩写DOM DOM是针对HTML和XML文档的一个API(应用程序编程接口).DOM描绘 ...
- javascript之DOM(Document Object Model) 文档对象模型
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 《javascript高级程序设计》笔记:文档模式
文档模式是用于指定浏览器使用什么样的标准来正确的显示网页,各个标准的解析存在着差异 文档类型的分类 文档模式大致分为三种类型: 混杂模式(quirks mode) 标准模式(standards mod ...
随机推荐
- spring mvc标准项目结构
src com.xxx.inews.dao com.xxx.inews.dao.impl com.xxx.inews.data.entity com.xxx.inews.data.vo com.xxx ...
- 自然语言处理(1)之NLTK与PYTHON
自然语言处理(1)之NLTK与PYTHON 题记: 由于现在的项目是搜索引擎,所以不由的对自然语言处理产生了好奇,再加上一直以来都想学Python,只是没有机会与时间.碰巧这几天在亚马逊上找书时发现了 ...
- 转:使用Android API最佳实践
原文来自于:http://blog.jobbole.com/65170/ 写在前面 现在,Android应用程序中集成第三方API已十分流行.应用程序都有自己的网络操作和缓存处理机制,但是大部分比较脆 ...
- IDE 常用快捷键记录
一:Eclipse (1)删除当前行:Ctrl+D (2)最大化当前编辑窗口:Ctrl+m (3)关闭当前编辑器窗口:Ctrl+F4/Ctrl+w (4)导入依赖包:Ctrl+Shift+o 二:Ne ...
- INDEX RANG SCAN无需回表的情况
create table a3 as select * from dba_objects create index a3_idx1 on a3(owner); select owner from a3 ...
- HDU4666 Hyperspace(曼哈顿)
题目链接. 分析: 这是多校的一个题,当时没做出来.学长说让用multiset. 用multiset将每一个数的1<<dim个状态全部保存.假设状态 i, 最远曼哈顿距离应当是 max[i ...
- sql(SqlServer)编程基本语法
一.定义变量 --简单赋值 declare @a int set @a=5 print @a --使用select语句赋值 declare @user1 nvarchar(50) select @ ...
- gtest官方文档浅析
gtest的所有官方文档:http://code.google.com/p/googletest/w/list 选择单元测试框架的那些事 gtest不是唯一开源的单元测试框架,我也不觉得它是最好的单元 ...
- Matlab:拟合(1)
拟合练习: function f = curvefun(x, tdata) f = (x()*x()*x()) / (x()-x()) * ( exp(-x()*tdata)/(x()-x()) + ...
- codeforces 341C Iahub and Permutations(组合数dp)
C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...