Home Web Front-end JS Tutorial Website Development | JavaScript Tutorials

Website Development | JavaScript Tutorials

Feb 07, 2017 pm 05:17 PM

JavaScript is the most popular programming language in the world. This language can be used in HTML and the web, and can be used on a wide range of devices such as servers, PCs, laptops, tablets, and smartphones.

JavaScript is a scripting language

JavaScript is a lightweight programming language.

JavaScript is programming code that can be inserted into HTML pages.

JavaScript, when inserted into an HTML page, can be executed by all modern browsers.

JavaScript: Writing HTML Output

Example

document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph</p>");
Copy after login

Tip: You can only use document.write with HTML output. If you use this method after the document is loaded, the entire document will be overwritten.

JavaScript: Reacting to Events

Example

<button type="button" onclick="alert(&#39;Welcome!&#39;)">点击这里</button>
Copy after login

alert() function is not commonly used in JavaScript, but it is very convenient for code testing . The

onclick event is just one of many events you'll learn about in this tutorial.

JavaScript: Changing HTML content

Using JavaScript to process HTML content is a very powerful feature.

Example

x=document.getElementById("demo")  //查找元素x.innerHTML="Hello JavaScript";    //改变内容
Copy after login

You will often see document.getElementByID("some id"). This method is defined in the HTML DOM. DOM (Document Object Model) is the official W3C standard for accessing HTML elements.

JavaScript: Change HTML style

Changing the style of HTML elements is a variant of changing HTML attributes.

Example

x=document.getElementById("demo")  //找到元素x.style.color="#ff0000";           //改变样式
Copy after login

JavaScript: Validating input

JavaScript is often used to validate user input.

实例

if isNaN(x) {alert("Not Numeric")};
Copy after login

您知道吗?

提示:JavaScript 与 Java 是两种完全不同的语言,无论在概念还是设计上。

Java(由 Sun 发明)是更复杂的编程语言。

ECMA-262 是 JavaScript 标准的官方名称。

JavaScript 由 Brendan Eich 发明。它于 1995 年出现在 Netscape 中(该浏览器已停止更新),并于 1997 年被 ECMA(一个标准协会)采纳。

JavaScript 使用

HTML 中的脚本必须位于 <script> 与 </script> 标签之间。

脚本可被放置在 HTML 页面的 和 部分中。

<script> 标签</strong></p><p>如需在 HTML 页面中插入 JavaScript,请使用 <script> 标签。</p><p><script> 和 </script> 会告诉 JavaScript 在何处开始和结束。

<script> 和 </script> 之间的代码行包含了 JavaScript:

<script>
alert("My First JavaScript");
</script>
Copy after login

您无需理解上面的代码。只需明白,浏览器会解释并执行位于 <script> 和 </script> 之间的 JavaScript。

那些老旧的实例可能会在 <script> 标签中使用 type="text/javascript"。现在已经不必这样做了。JavaScript 是所有现代浏览器以及 HTML5 中的默认脚本语言。</p><p style="margin: 0px; padding: 0px; font-weight: 400; font-size: 16px; max-width: 100%; color: rgb(62, 62, 62); font-family: -apple-system-font, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif; white-space: normal; background-color: rgb(255, 255, 255); box-sizing: border-box !important; word-wrap: break-word !important;"><body> 中的 JavaScript</p><p>在本例中,JavaScript 会在页面加载时向 HTML 的 <body> 写文本:</p><p style="margin: 20px 0px 0px; padding: 0px; font-size: 12px; max-width: 100%; color: rgb(62, 62, 62); font-family: -apple-system-font, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif; white-space: normal; background-color: rgb(255, 255, 255); border: 0px; box-sizing: border-box !important; word-wrap: break-word !important;">实例</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><!DOCTYPE html> <html> <body> . . <script> document.write(&quot;&lt;h1&gt;This is a heading&lt;/h1&gt;&quot;); document.write(&quot;&lt;p&gt;This is a paragraph&lt;/p&gt;&quot;); </script> . .

Copy after login

JavaScript 函数和事件

上面例子中的 JavaScript 语句,会在页面加载时执行。

通常,我们需要在某个事件发生时执行代码,比如当用户点击按钮时。

如果我们把 JavaScript 代码放入函数中,就可以在事件发生时调用该函数。

您将在稍后的章节学到更多有关 JavaScript 函数和事件的知识。


或 中的 JavaScript

您可以在 HTML 文档中放入不限数量的脚本。

脚本可位于 HTML 的 或 部分中,或者同时存在于两个部分中。

通常的做法是把函数放入 部分中,或者放在页面底部。这样就可以把它们安置到同一处位置,不会干扰页面的内容。


中的 JavaScript 函数

在本例中,我们把一个 JavaScript 函数放置到 HTML 页面的 部分。

该函数会在点击按钮时被调用:

实例

<!DOCTYPE html>
<html>

<head><script>
function myFunction()
{
document.getElementById("demo").innerHTML="My First JavaScript Function";
}
</script></head>

<body>

<h1>My Web Page</h1>

<p id="demo">A Paragraph</p>

<button type="button" onclick="myFunction()">Try it</button>

</body>
</html>
Copy after login

中的 JavaScript 函数

在本例中,我们把一个 JavaScript 函数放置到 HTML 页面的 部分。

该函数会在点击按钮时被调用:

实例

<!DOCTYPE html>
<html>
<body>

<h1>My Web Page</h1>

<p id="demo">A Paragraph</p>

<button type="button" onclick="myFunction()">Try it</button><script>
function myFunction()
{
document.getElementById("demo").innerHTML="My First JavaScript Function";
}
</script></body>
</html>
Copy after login

提示:我们把 JavaScript 放到了页面代码的底部,这样就可以确保在

元素创建之后再执行脚本。

外部的 JavaScript

也可以把脚本保存到外部文件中。外部文件通常包含被多个网页使用的代码。

外部 JavaScript 文件的文件扩展名是 .js。

如需使用外部文件,请在

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles