当前位置:网站首页>2、 Three ways to write JS code

2、 Three ways to write JS code

2022-06-13 08:33:00 001128XY

Write JS Three ways to code

1、 Inline
Format :

<button onclick="alert('hello js');"> Am I </button>

2、 Inside JS( In page )
Format :

<!-- type="text/javascript"  Indicates that the type of script you write is JS -->
<!--  If you don't write , The default is type="text/javascript" -->
<script type="text/javascript"> alert("hello js"); </script>

Be careful : Advice on body Bottom , Because the code is executed from top to bottom , stay head The label says JS Code can't get body The elements of the label , If you want to write in front , Think again HTML Elements in can be used window.onload.

3、 external JS( Outer chain )
Format :

<script type="text/javascript" src="out.js"></script>

Be careful : Outer chain script You cannot write in a code block js Code , Even if it's written, it won't execute ;

<script type="text/javascript" src="out.js"> alert("helo js~"); //  Will not be executed  </script>
原网站

版权声明
本文为[001128XY]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270540386107.html