02-js用法

1. 直接写在script标签对中
<script>
    alert('hello world');
</script>
2. 通过事件的方式写js代码
<body>
    <button onclick="test()">按钮</button>
</body>
<script>
    function test(){
        alert('hello world');
    }
</script>
<body>
    <button onclick="alert('hello world');">按钮</button>
</body>
3. 通过协议的方式写js代码
<a href="javascript:alert('hello world')">去百度</a>
<a href="javascript:void(0)" onclick="">没有反应的a链接</a>
<a href="http://www.baidu.com" onclick="return confirm('想去百度')">去百度</a>
4. 引入外部js文件
# js.js文件内容
alert('hello world')

# html文件引入js.js文件
<script src="js.js"></script>

猜你喜欢

转载自blog.csdn.net/rulaixiong/article/details/80664715
今日推荐