JavaScript HTML DOM

当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model)。

HTML DOM 模型被构造为对象的树,如图:

通过可编程的对象模型,JavaScript 获得了足够的能力来创建动态的 HTML。

  • JavaScript 能够改变页面中的所有 HTML 元素
  • JavaScript 能够改变页面中的所有 HTML 属性
  • JavaScript 能够改变页面中的所有 CSS 样式
  • JavaScript 能够对页面中的所有事件做出反应

查找 HTML 元素

  • 通过id查找HTML元素。用方法:document.getElementById(id_name);
  • 通过标签名查找HTML元素。用方法:getElementsByTagName(tag_name);
  • 通过类名查找 HTML 元素在 IE 5,6,7,8 中无效。这里暂不讨论

DOM 改变 HTML 输出流

<!DOCTYPE html>
<html>
<body> <script>
document.write(Date()); //绝不要使用在文档加载之后使用 document.write()。这会覆盖该文档。
</script> </body>
</html>

改变 HTML 内容

语句:document.getElementById(id).innerHTML=new HTML

<html>
<body> <p id="p1">Hello World!</p> <script>
document.getElementById("p1").innerHTML="New text!";
</script> </body>
</html>

改变 HTML 属性

<!DOCTYPE html>
<html>
<body> <img id="image" src="smiley.gif"> <script>
document.getElementById("image").src="landscape.jpg";
</script> </body>
</html>

改变 HTML 样式

语句: document.getElementById(id).style.property=new style

<!DOCTYPE html>
<html>
<body> <h1 id="id1">My Heading 1</h1>
<button type="button"
onclick="document.getElementById('id1').style.color='red'">
Click Me!</button> </body>
</html>

对事件做出反应

我们可以在事件发生时执行 JavaScript,比如当用户在 HTML 元素上点击时。

如需在用户点击某个元素时执行代码,请向一个 HTML 事件属性添加 JavaScript 代码:onclick=JavaScript

HTML 事件的例子:

  • 当用户点击鼠标时
  • 当网页已加载时
  • 当图像已加载时
  • 当鼠标移动到元素上时
  • 当输入字段被改变时
  • 当提交 HTML 表单时
  • 当用户触发按键时

点击文本内容改变文本:

<!DOCTYPE html>
<html>
<body>
<h1 onclick="this.innerHTML='Ooops!'">Click on this text!</h1>
</body>
</html>

点击按键改变文本内容:

<!DOCTYPE html>
<html>
<head>
<script>
function changetext(id)
{
id.innerHTML="Ooops!";
}
</script>
</head>
<body>
<h1 onclick="changetext(this)">Click on this text!</h1>
</body>
</html>

点击按键文本内容:

<html>
<body>
<p>点击按钮执行 <em>displayDate()</em> 函数.</p>
<button onclick="displayDate()">点这里</button>
<script>
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
<p id="demo"></p>
</body>
</html>

给按钮定义一个事件:

<html>
<head>
</head>
<body>
<p>点击按钮执行 <em>displayDate()</em> 函数.</p>
<button id="myBtn">点这里</button>
<script>
document.getElementById("myBtn").onclick=function(){displayDate()};
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
<p id="demo"></p>
</body>
</html>

鼠标在文本上,不在文本上,鼠标点击,鼠标起来。

<html>
<body>
<div onmouseover="mOver(this)" onmouseout="mOut(this)" style="background-color:#D94A38;width:120px;height:20px;padding:40px;">Mouse Over Me</div>
<div onmousedown="mOver(this)" onmouseup="mOut(this)" style="background-color:#D94A38;width:120px;height:20px;padding:40px;">Mouse Over Me</div>
<script>
function mOver(obj)
{
obj.innerHTML="Thank You"
} function mOut(obj)
{
obj.innerHTML="Mouse Over Me"
}
</script>
</body>
</html>

创建新的 HTML 元素

<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div> <script>
var para=document.createElement("p");
var node=document.createTextNode("This is new.");
para.appendChild(node); var element=document.getElementById("div1");
element.appendChild(para);
</script>

删除已有的 HTML 元素

<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script>
var parent=document.getElementById("div1");
var child=document.getElementById("p1");
parent.removeChild(child);
</script>

最新文章

  1. [MySQL Reference Manual] 23 Performance Schema结构
  2. CUDA ---共享存储器
  3. Android RadioGroup设置默认选中项
  4. iframe 使用
  5. sql分组取第一条数据
  6. navicat 的查询功能
  7. TCP拥塞控制
  8. C# 中控件 WebBrowser 对 frameset/ iframe 操作和内容获取
  9. 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context}
  10. java Script 用if else 实现从大到小指定输出,升序排列
  11. 在可编辑div的光标下插入html
  12. 4.Django模板语言和分页
  13. 2017-2018 Northwestern European Regional Contest (NWERC 2017)
  14. 万能分布式消费框架,添加基于redis中间件的方式。
  15. RocketMQ入门(Filter)_5
  16. Docker 简介,入门
  17. EXT4 超级块介绍(转)
  18. 17.scrapy-splash安装-2
  19. 各种initcall的执行先后顺序(module_init、postcore_initcall、arch_initcall、subsys_initcall、 fs_initcall)【转】
  20. 记录C#错误日志工具

热门文章

  1. Chrome Command API
  2. Linux修改命令行样式
  3. table表格中 ,点击checkbox 的value值 相加
  4. GitHub Desktop使用
  5. Java基础 深拷贝浅拷贝
  6. PAT 1028. List Sorting
  7. java压缩与解压
  8. 基于Tag的Docker自动构建
  9. 【创客+】偷心锁屏创始人Jerry创业心得分享
  10. 微信小程序开发2-第一个小程序开发准备