HTML DOM 使 JavaScript 有能力对 HTML 事件做出反应。
我们可以在事件发生时执行 JavaScript,比如当用户在 HTML 元素上点击时。
HTML 事件的例子:
当用户点击鼠标时
当网页已加载时
当图像已加载时
当鼠标移动到元素上时
当输入字段被改变时
当提交 HTML 表单时
当用户触发按键时
下面的实例中,当用户在 <h1> 元素上点击时,会改变其内容:
<h1 onclick="this.innerHTML="谢谢!"">请点击该文本</h1>下面的实例从事件处理器调用一个函数:
<!DOCTYPE html> <html> <head> <script> function changetext(id) { id.innerHTML="谢谢!"; } </script> </head> <body> <h1 onclick="changetext(this)">请点击该文本</h1> </body> </html>
<!DOCTYPE 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>注释:在上面的例子中,名为 displayDate 的函数将在按钮被点击时执行。
<!DOCTYPE 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>注释:在上面的例子中,名为 displayDate 的函数被分配给 id=myButn" 的 HTML 元素。
<!DOCTYPE html> <html> <head> <script> function mymessage() { alert("这段消息由 onload 事件触发"); } </script> </head> <body onload="mymessage()"> </body> </html>onload 事件可用于检测浏览器类型和浏览器版本,并基于这些信息来加载网页的正确版本。
<!DOCTYPE html> <html> <body onload="checkCookies()"> <script> function checkCookies() { if (navigator.cookieEnabled==true) { alert("已启用 cookie") } else { alert("未启用 cookie") } } </script> <p>提示框会告诉你,浏览器是否已启用 cookie。</p> </body> </html>
<!DOCTYPE html> <html> <head> <script> function myFunction() { var x=document.getElementById("fname"); x.value=x.value.toUpperCase(); } </script> </head> <body> 请输入英文字符: <input type="text" id="fname" onchange="myFunction()"> <p>当您离开输入字段时,会触发将输入文本转换为大写的函数。</p> </body> </html>
<h1 onmouseover="style.color="red"" onmouseout="style.color="blue""> 请把鼠标移到这段文本上 </h1>下面是一个简单的 onmouseover-onmouseout 实例:
<!DOCTYPE html> <html> <body> <div onmouseover="mOver(this)" onmouseout="mOut(this)" style="background-color:green;width:120px;height:20px;padding:40px;color:#ffffff;"> 把鼠标移到上面 </div> <script> function mOver(obj) { obj.innerHTML="谢谢" } function mOut(obj) { obj.innerHTML="把鼠标移到上面" } </script> </body> </html>
<!DOCTYPE html> <html> <body> <div onmousedown="mDown(this)" onmouseup="mUp(this)" style="background-color:green;color:#ffffff;width:90px;height:20px;padding:40px;font-size:12px;"> 请点击这里 </div> <script> function mDown(obj) { obj.style.backgroundColor="#1ec5e5"; obj.innerHTML="请释放鼠标按钮" } function mUp(obj) { obj.style.backgroundColor="green"; obj.innerHTML="请按下鼠标按钮" } </script> </body> </html>下面的实例中,当用户按下鼠标按钮时,更换一幅图像。
<!DOCTYPE html> <html> <head> <script> function lighton() { document.getElementById("myimage").src="../lighton.gif"; } function lightoff() { document.getElementById("myimage").src="../lightoff.gif"; } </script> </head> <body> <img id="myimage" onmousedown="lighton()" onmouseup="lightoff()" src="../lightoff.gif" /> <p>按住鼠标不放可以点亮这盏灯!</p> </body> </html>
<!DOCTYPE html> <html> <head> <script> function myFunction(x) { x.style.background="yellow"; } </script> </head> <body> 请输入英文字符: <input type="text" onfocus="myFunction(this)"> <p>当输入字段获得焦点时,会触发改变背景颜色的函数。</p> </body> </html>
评论