PHP前端开发

内联JavaScript如何与HTML一起工作?

百变鹏仔 4周前 (09-22) #HTML
文章标签 内联

在本文中,您将了解内联 JavaScript 如何与 HTML 配合使用。内联 JavaScript 表示在 html 文件中的 <script> 标记之间编写的代码块。在 HTML 文件中使用内联 JavaScript 的优点是减少 Web 浏览器到服务器的往返。</script>

示例 1

让我们了解如何使用内联 JavaScript 在 html 文件中添加基本语句 -

<!DOCTYPE html ><html><head>   <h2> This is a simple HTML file</h2>   <script>      document.write("Hi, This is an inline Javascript code written in between script tags");   </script></head></html>

说明

  • 第 1 步 - 在一个简单的 html 文件中,添加标题标签

    并显示您选择的标题。

  • 步骤 2 - 在标题标签下方添加脚本标签 <script>。</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/c1c2c2ed740f" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Java免费学习笔记(深入)</a>”;</p></li><li><p><b>步骤 3</b> -在 script 标签中,使用 document.write() 函数打印文本。</p></li></ul><h2>示例 2</h2><p>在此示例中,让我们使用内联 JavaScript 打印 html 文件中的数组。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>&lt;!DOCTYPE html &gt;&lt;html&gt;&lt;head&gt; &lt;h2&gt; This is a simple HTML file&lt;/h2&gt; &lt;script&gt; document.write(&quot;An Inline Javascript code to display an array&quot;) document.write(&quot;&lt;br&gt;&quot;) const inputArray = [3,4,5,6,7,8] document.write(&quot;The array is defined as :&quot;, inputArray) &lt;/script&gt;&lt;/head&gt;&lt;/html&gt;</pre><div class="contentsignin"></div></div><h2>说明</h2><ul class="list"><li><p><b>第 1 步</b> - 在一个简单的 html 文件中,添加标题标签 </p><h2> 并显示您选择的标题。</h2></li><li><p><b>步骤 2</b> - 在标题标签下方添加脚本标签 <script>。</script>

    立即学习“Java免费学习笔记(深入)”;

  • 步骤3 -在脚本标签中,定义一个数组,即inputArray,并向该数组添加数值。

  • 第 4 步 - 使用 document.write() 函数打印数组。