向 button 元素分配 onclick 事件: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>
2.瀏覽器
<!DOCTYPE html> <html> <body onload = "checkCookies()"> <script> function checkCookies() { try { if (navigator.cookieEnabled == true) { alert("已啓用 cookie") } else { alert("未啓用 cookie") } } catch (err) { alert('asdf') } } </script> <p>提示框會告訴你,瀏覽器是否已啓用 cookie。</p> </body> </html>
3. cookie
<!DOCTYPE html> <html> <head> <script> function myF() { var x = document.getElementById("fname"); x.value = x.value.toUpperCase(); } </script> </head> <body> 請輸入應爲字符:<input type = "text" id = "fname" onchange = "myF()"> <p>當您離開輸入字段時,會觸發將輸入文本轉換爲大寫的函數。</p> </body> </html>