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

PHPz
PHPz 转载
2023-09-01 14:33:06 141浏览

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

在本文中,您将了解内联 JavaScript 如何与 HTML 配合使用。内联 JavaScript 表示在 html 文件中的

示例 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 - 在标题标签下方添加脚本标签

  • 步骤 3 -在 script 标签中,使用 document.write() 函数打印文本。

示例 2

在此示例中,让我们使用内联 JavaScript 打印 html 文件中的数组。

<!DOCTYPE html >
<html>
<head>
   <h2> This is a simple HTML file</h2>
   <script>
      document.write("An Inline Javascript code to display an array")
      document.write("<br>")
      const inputArray = [3,4,5,6,7,8]
      document.write("The array is defined as :", inputArray)
   </script>
</head>
</html>

说明

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

    并显示您选择的标题。

  • 步骤 2 - 在标题标签下方添加脚本标签

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

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

以上就是内联JavaScript如何与HTML一起工作?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除