日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JavaScript代码在哪里放置?

發(fā)布時間:2023/12/14 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaScript代码在哪里放置? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

In an HTML code, JavaScript can be placed at three places...

在HTML代碼中,JavaScript可以放在三個位置...

  • Between the <head>...</head> tag

    在<head> ... </ head>標(biāo)記之間

  • Between the <body>...</body> tag

    在<body> ... </ body>標(biāo)記之間

  • In the external file with an extension ".js"

    在擴(kuò)展名為“ .js”的外部文件中

  • 在<head> ... </ head>和<body> ... </ body>標(biāo)記之間 (Between the <head>...</head> and <body>...</body> tags)

    If we want to place a JavaScript code in an HTML file, we can place either in <head>...</head> tag or <body>...</body> tag.

    如果要在HTML文件中放置JavaScript代碼,則可以將其放置在<head> ... </ head>標(biāo)記或<body> ... </ body>標(biāo)記中。

    Syntax:

    句法:

    <script type="text/javascript">//js code </script>

    Example:

    例:

    <html> <head><title>JavaScipt Example</title><script type="text/javascript">//function to add two numbersfunction sum(a, b){return (a+b);}</script> </head><body><script type="text/javascript">//function to subtract two numbersfunction sub(a, b){return (a-b);}</script><p>Here, we are printing sum & sum...</p><p id="result1"></p><p id="result2"></p><script type="text/javascript">//calling functions & printing the valuesdocument.getElementById("result1").innerHTML = "sum = " + sum(10,20);document.getElementById("result2").innerHTML = "sub = " + sub(10,20);</script> </body> </html>

    Output:

    輸出:

    Here, we are printing sum & sum...sum = 30sub = -10

    在擴(kuò)展名為“ .js”的外部文件中 (In the external file with an extension ".js")

    We can also write JavaScript code in an external file, and import the file in an HTML file.

    我們還可以在外部文件中編寫JavaScript代碼,并在HTML文件中導(dǎo)入文件。

    Syntax to import JavaScript file in an HTML file:

    在HTML文件中導(dǎo)入JavaScript文件的語法:

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

    Example:

    例:

    JavaScript file (functions.js)

    JavaScript文件(functions.js)

    //function to add two numbers function sum(a, b){return (a+b); }//function to subtract two numbers function sub(a, b){return (a-b); }

    HTML file

    HTML文件

    <html> <head><title>JavaScipt Example</title><script src="functions.js"></script> </head><body><p>Here, we are printing sum & sum...</p><p id="result1"></p><p id="result2"></p><script type="text/javascript">//calling functions & printing the valuesdocument.getElementById("result1").innerHTML = "sum = " + sum(10,20);document.getElementById("result2").innerHTML = "sub = " + sub(10,20);</script> </body> </html>

    Output:

    輸出:

    Here, we are printing sum & sum...sum = 30sub = -10

    翻譯自: https://www.includehelp.com/code-snippets/where-to-place-javascript-code.aspx

    總結(jié)

    以上是生活随笔為你收集整理的JavaScript代码在哪里放置?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。