Javascript - Basics (1)

Javascript three reference methods

Javascript-------inline

Inline references should be used in conjunction with events in tags, and internal and external chains cannot be used.

 <button onclick="alert('点击我了')">点击我</button>

 

Javascript-------inner chain (embedded)

Syntax: <script>  Write js code here </script>

The inner link is written under the root element of html (recommended). It is also possible to write in the body tag. It is not recommended to write in the head tag now. The code is to compile one line and execute one line. Writing in the head tag will cause a top-heavy effect, so It will make a code file compile js first and then compile html. The order is wrong and it is easy to cause problems. After delay loading is introduced later, this problem can be solved.

 <script>
        document.write(123); //内容写在body中
        console.log(123); //内容在后台浏览器中
        console.log("hello,js");//字符串双引号
        alert("这是内链式");//这是弹出框
    </script>

 

 Javascript-------external link

Syntax: <script src="Write the relative path of your js file here"> </script>

<script src="js/public.js"></script>

 js file 

alert('这是外链式');

Note: For files imported by src, js code cannot be written in the external link, that is, the srcipt tag cannot be written in the middle of the external link. The code execution order is top-down by default, and multiple script tags can appear on a page But a script tag can only have one src path, it is recommended to write the inner link after the root html element.

 Javascript comments

Single-line comments: //Here is the js code, and the entire line will be commented.

Shortcut key: ctrl+/

//这是js的单行注释代码

Multi-line comment: /* This is multi-line js code */, multi-line comment starts with /* and ends with */, any code in it will not be executed.

Shortcut key: Alt+Shift+a

/*这里是js多行代码注释
    1
    2
    3
      */
 <script>
        document.write(123); //内容写在body中
        console.log(123); //内容在后台浏览器中
        /* console.log("hello,js");//字符串双引号
        alert("这是内链式");//这是弹出框 */
    </script>

Note: Single-line comments are the most commonly used. Select all the code to be commented, and the shortcut key Ctrl+/ can quickly comment multiple lines of code, especially when the code is tested, the comment can block the code.

                                                                            Everyone is welcome to correct mistakes. If you have any questions, please feel free to chat with me privately.

Guess you like

Origin blog.csdn.net/apple_52282345/article/details/126519541