javascript弹出框_了解JavaScript第2部分(变量和弹出框简介)

javascript弹出框

Hello,

你好,

It's been long time since I wrote my last article on this series. You may like to read first part of this series named Understanding-javasript-part-1-getting-started. Let's continue on with part 2. Today you will learn how to declare/create variables, how to use them, what are their types, and what are pop-up boxes.

自从我写了关于该系列的上一篇文章以来已经很长时间了。 您可能想阅读本系列的第一部分,名为谅解-javasript-pa rt-1-getti ng开始 。 让我们继续第2部分。今天,您将学习如何声明/创建变量,如何使用它们,它们的类型是什么以及什么是弹出框。

*变量简介: (*Introduction to Variables:)

什么是变量? 变量不过是我们存储值的结构。 变量在Javascript中很重要。 您应该熟悉这个术语。 不用担心,到本主题结束时,您将更加熟悉变量。

创建变量: (Creating Variables:)

To create variable there is syntax you should be familiar with:

要创建变量,您应该熟悉以下语法:

Syntax:

语法

var your_variable_name = "string" or (number);
Notes, 笔记,

Variables are declared by '

变量由'声明

var' keyword. It is like a some set of characters which help browser to recognized that you are declaring variable. var '关键字。 就像一组字符一样,这​​些字符可以帮助浏览器识别您在声明变量。

After 'var' keyword you can declare name of your variable. Variables name can be anything you like,but make sure you are declaring a name which will further help you to recognized easily what you stored in that particular variable

在“ var”关键字之后,您可以声明变量的名称。 变量名称可以是您喜欢的任何名称,但请确保声明一个名称,这将进一步帮助您轻松识别存储在该特定变量中的内容

After that values must be assign to variables. Use '=' assignment operator to assign values to variable.

之后,必须将值分配给变量。 使用“ =”赋值运算符将值赋给变量。

You can also declare variable first and then assign value to it later in a script.

您也可以先声明变量,然后再在脚本中为其赋值。

You can assign anything to variable like numbers ,strings,any data etc.

您可以为变量分配任何内容,例如数字,字符串,任何数据等。



*Strings*字符串

*Numbers:*电话号码:

*Data:*数据:

And lastly use semicolon (;) to finished declaring variable.

最后使用分号(;)完成变量的声明。

Example

var (then) my_first_variable = 'this is var';

To check what is variable use 'alert' command to check this out.

要检查什么是变量,请使用“ alert ”命令进行检查。

alert(my_first_variable);

You will get whatever contain you defined variable in output screen.In this case you will get 'this is var'

您将在输出屏幕中获得包含您定义的变量的任何内容。在这种情况下,您将获得``this is var''

*弹出框 (*Pop-up boxes)

在进一步介绍之前,让我们先看一下弹出框,

Like alert which you know very well now and which use to show alert messages to user we have two more commands available which works very much similar to alert.

警报一样,你现在非常清楚,并且用它来自动显示警告讯息给用户,我们有两个可用命令其工作方式非常类似警告。

Prompt 提示

Confirm. 确认

1.提示 (1. Prompt)

在提示时,您可以向用户提问,而无需回答,他无法在您的网站上走得更远。 该提示框对于获取用户名或城市名等很有用。

Example

prompt('Please enter your name','xyz')

Here in above code,first string, i.e. in between quotes ('') will ask a user his name and second string will give some default value to it so that user can change it or leave it as it is. However second one is optional.

在上面的代码中,第一个字符串,即在引号('')之间将询问用户他的名字,第二个字符串将为其提供一些默认值,以便用户可以更改它或保持原样。 但是第二个是可选的。

The output of above will be,

上面的输出将是,

Prompt Box

2.确认 (2. Confirm)

Confirm command can use to ask user whether he want to continue on or not. Let's take an example,

Confirm命令可用于询问用户是否要继续。 让我们举个例子

confirm('This will continue to next page,'+'\n'+' Do you want to continue??');

Then user will get two options ('OK','cancel') then you can use Javascript to execute bit of code if user clicks on 'OK' or execute another bit of code if he clicks 'cancel' etc.

然后,用户将获得两个选项(“确定”,“取消”),然后,如果用户单击“确定”,则可以使用Javascript执行部分代码;如果单击“取消”,则可以执行另一部分代码,等等。

Here how it will look,

这里看起来如何,

Confirm box

This above three commands commonly known as 'pop-up' boxes. Pop-up boxes normally use in demonstration purpose only. You should be careful while using this boxes on your remote website at a time this boxes may become frustrating to your users and they might leave your website,and that what you don't want at any cost. right? :-)

以上三个命令通常称为“ 弹出 ”框。 弹出框通常仅用于演示目的。 您应该小心,同时在您的远程网站上使用此框时,这些框可能会使您的用户感到沮丧,并且他们可能会离开您的网站,并且您不希望有任何损失。 对? :-)

*继续使用变量: (*Continue on with variables:)

var myElement = 'This is My variable';

*变量类型: (*Types of variables:)

1.全球 (1. Global)

2.当地 (2. Local)

Global Variables:

全局变量:

The defined Global variables are available to use anywhere in Javascript. The scope of global variables is far more larger than that of local variables. Declaring global variables are easy,

定义的全局变量可在Javascript中的任何地方使用。 全局变量的范围远大于局部变量的范围。 声明全局变量很容易,

<script type="text/Javascript">
	Var my_Global_Variable = "This is my global variable which i can use anywhere in Javascript"
</script> 

Local Variables:

局部变量:

Local variables are those who can not used anywhere in Javascript,they have some limitation. Local variables are declared in function. Their scope is small but they are far more important than global ones. You should always avoid to make global variables.

局部变量是那些在Javascript的任何地方都不能使用的变量,它们有一定的局限性。 局部变量在函数中声明。 它们的范围很小,但远比全球范围重要。 您应该始终避免创建全局变量。

<script type="text/Javascript">
	Function myfuncion() {
		Var my_local_Variable = "this variable is only used in this particular function"
	}
<script>

In simple words,

简单来说

Variables which are outside of functions are global variables and those who are inside functions are local variables.

函数外部的变量是全局变量,函数内部的变量是局部变量。

<script type="text/Javascript">

Var global_variable = "This is global variable";

Function myfunction () {
	Var myElement= "This is local variable";

}
</script>

*您应该记住的事情: (*Something you should keep remember:)

Variables must be start with keyword 'var'.

变量必须以关键字“ var”开头。

Variable can have any name you like such as myelement, myshop etc.

变量可以具有您喜欢的任何名称,例如myelement,myshop等。

Remember that variables name must not start with,

请记住,变量名称不得以开头



*Numbers, *数字

*Special characters like (%,^,&,!,#, etc), *特殊字符,例如(%,^,&,!,#等),

Variable name can start with any alphabet and special characters like _ $ .

变量名可以以任何字母和特殊字符(如_ $)开头。

Variables can have strings and numbers in it

变量中可以包含字符串和数字

Variables must have semicolon(;) at end of it

变量末尾必须带有分号(;)

Variables can call at any time any where in Javascript if they are global.

如果变量是全局变量,则可以随时在Javascript中的任何位置调用它们。

local variable is only valid inside of one specific function only where they are declared.

局部变量仅在声明一个特定函数的内部有效。

Whenever possible declared local variables only. Always try to avoid declaring global variables.

只要有可能,就仅声明局部变量。 始终尝试避免声明全局变量。



*摘要: (*Summary:)

到本文结束时,您已经具有声明和创建变量的基本知识。 您还知道如何自行创建弹出框,它们如何工作以及何时使用它们。 因此,请尝试练习它们并掌握本主题。接下来,用Java语言编写语句。

测验, (Quiz,)

Lets quickly take a quiz on variables and check if oyu can solve this question.The answers will be at end of this article.I will suggest you not to check answers without solving it first.

让我们快速地对变量进行测验,并检查oyu是否可以解决此问题。答案将在本文结尾处。我建议您在不首先解决它的情况下不要检查答案。

1.The variable name

1.变量名

var _myVariable is valid. 有效。

A) True

A)正确

B) False.

B)错误。

2.The variable name can start with a

[i]'$'

2,变量名可以以

[i]'$' [/i]sign? [/ i]签名?

A) False

A)错误

B) True

B)正确

3.Variables are of two types Local variables and ______ ??

3.变量有两种类型:局部变量和______?

A) Global

A)全球

B) Nobal

B)贵族

4.What is the purpose of prompt command??

4.提示命令的目的是什么?

A) To get a name from user and display it in webpage

A)从用户那里获取一个名称并将其显示在网页中

B) Ask user whether s/he want to continue on further.

B)询问用户是否要继续。

5.The syntax of variable is,

5,变量的语法是

A)int your_variable_name = "string" or (number)

A)

B)var your_variable_name = "string" or (number);

B)

Answers:

答案:

1: B

1: B

2: B

2: B

3: A

3:

4: A

4: A

5: B

5: B

Hope everybody have got full marks. If not then please go on and read this article again and specially grab variables concept perfectly. See you at next part.

希望每个人都有满分。 如果不是,请继续阅读本文,特别是完美地掌握变量概念。 下一部分见。

翻译自: https://www.experts-exchange.com/articles/10077/Understanding-JavaScript-part-2-Introdution-to-variables-and-pop-up-boxes.html

javascript弹出框

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值