查找html元素:
1、id查找 :getElementById()方法
如果找到该元素,则该方法将以对象的形式返回该元素。
如果未找到该元素,则 x 将包含 null。
2、标签名查找:getElementByTagName()方法
本例查找 id="main" 的元素,然后查找 "main" 中的所有 <p> 元素:
var x=document.getElementById("main"); var y=x.getElementsByTagName("p");
3、类名查找:通过类名查找 HTML 元素在 IE 5,6,7,8 中无效
html输出:document.write()
绝不要使用在文档加载之后使用 document.write()。这会覆盖该文档
改变html内容:document.getElementById(id).innerHTML = new html;
改变html属性:document.getElementById(id).attribute = new value ;
如:改变id为image的src属性
document.getElementById("image").src ="a.gif" ;
改变html样式:document.getElementById(id).style.property = new style ;
如:document.getElementById("a").style.color= "red" ;
显示/不显示元素
不显示:document.getElementById('p1').style.visibility="hidden"
显示:document.getElementById('p1').style.visibility="visible"
HTML 事件的例子:
- 当用户点击鼠标时
- 当网页已加载时
- 当图像已加载时
- 当鼠标移动到元素上时
- 当输入字段被改变时
- 当提交 HTML 表单时
- 当用户触发按键时
向 button 元素分配 onclick 事件:
button的id为mybtn , 将displayDate()方法分配给该元素
<script> document.getElementById("myBtn").οnclick=function(){displayDate()}; </script>