博客列表 >js进入到html的三种方式-2019年1月16日22点08分

js进入到html的三种方式-2019年1月16日22点08分

kszyd的博客
kszyd的博客原创
2019年01月18日 11:07:16816浏览

实例

<!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>

运行实例 »

点击 "运行实例" 按钮查看在线实例

 

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议