JavaScript For Beginners(转载)_基础知识
注:我对原文进行了编辑,对一些词汇标注颜色,方便阅读。本来准备翻译,但是觉得文章简单易懂,而且原文写得很好,所以就不献丑了。希望对JavaScript初学者能有所帮助。你可以跟着作者一起做那些示例代码,等读完文章的时候,你就可以掌握JavaScript的基本操作了,你会发现其实这一切很容易。
Contents
Embedding and including
write and writeln
Document object
Message box
Function
Event handler
Form
Link
Date
Window
Frame
Embedding and including
Let's first see a simple example:
head >
title > This is a JavaScript example title >
script language ="JavaScript" >
script >
head >
body > Hi, man! body >
html >
Usually, JavaScript code starts with the tag and ends with the tag . The code placed between and . Sometimes, people embed the code in the tags:
head > head >
body >
script >

script >
body >
html >
Why do we place JavaScript code inside comment fields <!--
and //-->
?
It's for ensuring that the Script is not displayed by old browsers that do not support JavaScript. This is optional, but considered good practice. The LANGUAGE attribute also is optional, but recommended. You may specify a particular version of JavaScript:
You can use another attribute SRC to include an external file containing JavaScript code:
For example, shown below is the code of the external file hello.js :
The external file is simply a text file containing JavaScript code with the file name extension ".js".
Note:
- Including an external file only functions reliably across platforms n the version 4 browsers.
- The code can't include tags and , or you will get an error message.
write and writeln
In order to output text in JavaScript you must use write() or writeln(). Here's an example:
HEAD >
TITLE > Welcome to my site TITLE > HEAD >
BODY >
SCRIPT LANGUAGE ="JAVASCRIPT" >
SCRIPT >
BODY >
HTML >
Note: the document object write is in lowercase as JavaScript is case sensitive. The difference between write and writeln is: write just outputs a text, writeln outputs the text and a line break.
Document object
The document object is one of the most important objects of JavaScript. Shown below is a very simple JavaScript code:
In this code, document is the object. write is the method of this object. Let's have a look at some of the other methods that the document object possesses.
lastModified
You can always include the last update date on your page by using the following code:document.write( " This page created by John N. Last update: " + document.lastModified);
script >
All you need to do here is use the lastModified property of the document. Notice that we used
<font color="#0000ff">+</font>
to put together This page created by John N. Last update: and document.write.
bgColor and fgColor
Lets try playing around with bgColor and fgColor:document.bgColor = " black "
document.fgColor = " #336699 "
script >
Message Box
alert
There are three message boxes: alert, confirm, and prompt. Let's look at the first one:script >
window.alert( " Welcome to my site! " )
script >
body >
You can put whatever you want inside the quotation marks.
confirm
An example for confirm box:prompt
Prompt box is used to allow a user to enter something according the promotion:In all our examples above, we wrote the box methods as window.alert(). Actually, we could simply write the following instead as:
confirm()
prompt()
Variables and Conditions
Let's see an example:
var x = window.confirm( " Are you sure you want to quit " )
if (x)
window.alert( " Thank you. " )
else
window.alert( " Good choice. " )
script >
There are several concepts that we should know. First of all, var x = is a variable declaration. If you want to create a variable, you must declare the variable using the var statement. x will get the result, namely, true or false . Then we use a condition statement if else to give the script the ability to choose between two paths, depending on this result (condition for the following action). If the result is true (the user clicked "ok"), "Thank you" appears in the window box. If the result is false (the user clicked "cancel"), "Good choice" appears in the window box instead. So we can make more complex boxes using var, if and those basic methods.
var y = window.prompt( " please enter your name " )
window.alert(y)
script >
Another example:
script >
var x = confirm( " Are you sure you want to quit? " )
if ( ! x)
window.location = " http://www.yahoo.com "
script >
head >
body >
Welcome to my website!.
body > html >
If you click "cancel", it will take you to yahoo, and clicking ok will continue with the loading of the current page "Welcome to my website!". Note: if(!x) means: if click "cancel". In JavaScript, the exclamation mark !means: "none".
Function
Functions are chunks of code.Let's create a simple function:
{
document.write("Hello can you see me?")
}
Note that if only this were within your <script> </script> tags, you will not see "Hello can you see me?" on your screen because functions are not executed by themselves until you call upon them. So we should do something:
{
document.write("Hello can you see me?")
}
test()
Last line test() calls the function, now you will see the words "Hello can you see me?".
Event handler
What are event handlers? They can be considered as triggers that execute JavaScript when something happens, such as click or move your mouse over a link, submit a form etc.
onClick
onClick handlers execute something only when users click on buttons, links, etc. Let's see an example:function ss()
{
alert( " Thank you! " )
}
script >
form >
input type ="button" value ="Click here" onclick ="ss()" >
form >
The function ss() is invoked when the user clicks the button. Note: Event handlers are not added inside the <script></script> tags, but rather, inside the html tags.
onLoad
The onload event handler is used to call the execution of JavaScript after loading:frameset onload ="ss()" >
img src ="whatever.gif" onload ="ss()" >
onMouseover,onMouseout
These handlers are used exclusively with links.a href ="#" onMouseOut ="alert('Good try!')" > Get Out Here! a >
onUnload
onUnload executes JavaScript while someone leaves the page. For example to thank users.Handle multiple actions
How do you have an event handler call multiple functions/statements? That's simple. You just need to embed the functions inside the event handler as usual, but separate each of them using a semicolon:input type ="button" value ="Click here!" onClick ="alert('Thanks for visiting my site!');window.location='http://www.yahoo.com'" >
form >
Form
Let's say you have a form like this:
input type ="text" size ="10" value ="" name ="bb" > br >
input type ="button" value ="Click Here" onclick ="alert(document.aa.bb.value)" >
form >
Notice that we gave the names to the form and the element. So JavaScript can gain access to them.
onBlur
If you want to get information from users and want to check each element (ie: user name, password, email) individually, and alert the user to correct the wrong input before moving on, you can use onBlur. Let's see how onBlur works:head >
script >
function emailchk()
{
var x = document.feedback.email.value
if (x.indexOf( " @ " ) ==- 1 )
{
alert( " It seems you entered an invalid email address. " )
document.feedback.email.focus()
}
}
script >
head >
body >
form
name ="feedback" >
Email: input type ="text" size ="20" name ="email"
onblur ="emailchk()" > br >
Comment: textarea name ="comment" rows ="2" cols ="20" > textarea > br >
input type ="submit" value ="Submit" >
form >
body >
html >
If you enter an email address without the @, you'll get an alert asking you to re-enter the data . What is: x.indexOf("@")==-1? This is a method that JavaScript can search every character within a string and look for what we want. If it finds it will return the position of the char within the string. If it doesn't, it will return -1. Therefore, x.indexOf("@")==-1basically means: "if the string doesn't include @, then:
document.feedback.email.focus()
What's focus() ? This is a method of the text box, which basically forces the cursor to be at the specified text box. onsubmit
Unlike onblur, onsubmit handler is inserted inside the tag, and not inside any one element. Lets do an example: script >
input type ="text" size ="20" name ="userName" >
input type ="text" size ="20" name ="password" >
input type ="submit" name ="submit" value ="Submit" >
form >
Note:
if(document.login.userName.value=="").This means "If the box named userName of the form named login contains nothing, then...". return false. This is used to stop the form from submitting. By default, a form will return true if submitting. return validate() That means, "if submitting, then call the function validate()
"
. Protect a file by using Login
Let's try an exampleSCRIPT Language ="JavaScript" >
function checkLogin(x)
{
if ((x.id.value != " Sam " ) || (x.pass.value != " Sam123 " ))
{
alert( " Invalid Login " );
return false ;
}
else
location = " main.htm "
}
script >
form >
p > UserID: input type ="text" name ="id" > p >
p > Password: input type ="password" name ="pass" > p >
p > input type ="button" value

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Kernelsecuritycheckfailure(内核检查失败)就是一个比较常见的停止代码类型,可蓝屏错误出现不管是什么原因都让很多的有用户们十分的苦恼,下面就让本站来为用户们来仔细的介绍一下17种解决方法吧。kernel_security_check_failure蓝屏的17种解决方法方法1:移除全部外部设备当您使用的任何外部设备与您的Windows版本不兼容时,则可能会发生Kernelsecuritycheckfailure蓝屏错误。为此,您需要在尝试重新启动计算机之前拔下全部外部设备。

如何使用WebSocket和JavaScript实现在线语音识别系统引言:随着科技的不断发展,语音识别技术已经成为了人工智能领域的重要组成部分。而基于WebSocket和JavaScript实现的在线语音识别系统,具备了低延迟、实时性和跨平台的特点,成为了一种被广泛应用的解决方案。本文将介绍如何使用WebSocket和JavaScript来实现在线语音识别系

WebSocket与JavaScript:实现实时监控系统的关键技术引言:随着互联网技术的快速发展,实时监控系统在各个领域中得到了广泛的应用。而实现实时监控的关键技术之一就是WebSocket与JavaScript的结合使用。本文将介绍WebSocket与JavaScript在实时监控系统中的应用,并给出代码示例,详细解释其实现原理。一、WebSocket技

如何使用WebSocket和JavaScript实现在线预约系统在当今数字化的时代,越来越多的业务和服务都需要提供在线预约功能。而实现一个高效、实时的在线预约系统是至关重要的。本文将介绍如何使用WebSocket和JavaScript来实现一个在线预约系统,并提供具体的代码示例。一、什么是WebSocketWebSocket是一种在单个TCP连接上进行全双工

Win10skype可以卸载吗是很多用户们都想知道的一个问题,因为很多的用户们发现自己电脑上的默认程序上有这个应用,担心删除后会影响到系统的运行,下面就让本站来为用户们来仔细的介绍一下Win10如何卸载SkypeforBusiness吧。Win10如何卸载SkypeforBusiness1、在电脑桌面点击Windows图标,再点击设置图标进入。2、点击“应用”。3、在搜索框中输入“Skype”,点击选中找到的结果。4、点击“卸载”。5

如何利用JavaScript和WebSocket实现实时在线点餐系统介绍:随着互联网的普及和技术的进步,越来越多的餐厅开始提供在线点餐服务。为了实现实时在线点餐系统,我们可以利用JavaScript和WebSocket技术。WebSocket是一种基于TCP协议的全双工通信协议,可以实现客户端与服务器的实时双向通信。在实时在线点餐系统中,当用户选择菜品并下单

JavaScript教程:如何获取HTTP状态码,需要具体代码示例前言:在Web开发中,经常会涉及到与服务器进行数据交互的场景。在与服务器进行通信时,我们经常需要获取返回的HTTP状态码来判断操作是否成功,根据不同的状态码来进行相应的处理。本篇文章将教你如何使用JavaScript获取HTTP状态码,并提供一些实用的代码示例。使用XMLHttpRequest

JavaScript和WebSocket:打造高效的实时天气预报系统引言:如今,天气预报的准确性对于日常生活以及决策制定具有重要意义。随着技术的发展,我们可以通过实时获取天气数据来提供更准确可靠的天气预报。在本文中,我们将学习如何使用JavaScript和WebSocket技术,来构建一个高效的实时天气预报系统。本文将通过具体的代码示例来展示实现的过程。We
