实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>js三种引入方式</title> </head> <body> <!-- 直接在元素的事件方法属性上写js --> <h3 id="测试" onclick="alert(id)">js很有趣</h3> <form action=""> <input type="text" name="site" value="表单测试"> <button type="button" onclick="alert(site.value)">点我</button> </form> <hr> <!-- js脚本直接写在script标签中,仅限当前页面 --> <form action=""> <input type="text" name="username" placeholder="用户名"> <button type="button" onclick="check(username)">验证</button> </form> <script> function check(username) { if(username.value.length===0){ alert('空用户名'); } else{ alert('验证通过'); } } </script> <hr> <!-- 引入外部js文件 --> <form action=""> <input type="text" name="us" placeholder="用户"> <button type="button" onclick="check(us)">测试</button> </form> <script src="js.js"></script> <hr><hr> <!-- 案例 --> <div id="qiu"></div> <style> #qiu { width: 80px; height: 80px; background-color: brown; border-radius: 50%; box-shadow: 3px 3px 3px lightblue; position: relative; } </style> <script> var qiu=document.getElementById('qiu'); function running(){ var count=0; var timer=setInterval( function (){ if(count <100) { qiu.style.left=qiu.style.top=count+'px'; count++; } else { clearInterval(timer); } }, 100); } qiu.addEventListener('click',running,false); </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例