js坚持不懈之16:使用js向HTML元素分配事件
程序员文章站
2023-08-18 09:38:30
向 button 元素分配 onclick 事件: 2. 3. ......
向 button 元素分配 onclick 事件:
<!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.
<!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>