2、js编写位置

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <!--
            1.可以将js代码编写到外部js文件中,然后通过script标签引入
            写到外部文件中可以在不同的页面中同时引用,也可以利用到浏览器的缓存机制
            推荐使用的方式 -->
        <script type="text/javascript" src="js/script.js">
            //alert("我是内部的JS代码"); 写也没用,浏览器会忽略
            //script标签一旦用于引入外部文件了,就不能再编写代码了,即使编写了浏览器也会忽略
        </script>
        
        <!--
            2.可以将js代码编写到script标签   --> 
        <script type="text/javascript">
            alert("我是script标签中的代码!!");
        </script>
    </head>
    
    <body>    
        <!--
            3.可以将js代码编写到标签的onclick属性中
            当我们点击按钮时,js代码才会执行
     
            虽然可以写在标签的属性中,但是他们属于结构与行为耦合,不方便维护,不推荐使用
     --> <button onclick="alert('讨厌,你点我干嘛~~');">点我一下</button> <!-- 4.可以将js代码写在超链接的href属性中,这样当点击超链接时,会执行js代码--> <a href="javascript:alert('让你点你就点!!');">你也点我一下</a> <a href="javascript:;">你也点我一下</a> </body> </html>

 

posted @ 2020-05-07 17:07  Arbitrary233  阅读(162)  评论(0编辑  收藏  举报