javascript 基础

警告框

警告框经常用于确保用户可以得到某些信息。

当警告框出现后,用户需要点击确定按钮才能继续进行操作。

语法:

alert("文本")

确认框

确认框用于使用户可以验证或者接受某些信息。

当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。

如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false

语法:

confirm("文本")

提示框

提示框经常用于提示用户在进入页面前输入某个值。

当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。

如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null

语法:

prompt("文本","默认值")

 

数组 for*  in  *):

<html>
<body>
 
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
 
for (x in mycars)
{
document.write(mycars[x] + "<br />")
}
</script>
 
</body>
</html>

 

onload onUnload

当用户进入或离开页面时就会触发 onload onUnload 事件。

onload 事件常用来检测访问者的浏览器类型和版本,然后根据这些信息载入特定版本的网页。

onload onUnload 事件也常被用来处理用户进入或离开页面时所建立的 cookies。例如,当某用户第一次进入页面时,你可以使用消息框来询问用户的姓名。姓名会保存在 cookie 中。当用户再次进入这个页面时,你可以使用另一个消息框来和这个用户打招呼:"Welcome John Doe!"

onFocus, onBlur onChange

onFocusonBlur onChange 事件通常相互配合用来验证表单。

下面是一个使用 onChange 事件的例子。用户一旦改变了域的内容,checkEmail() 函数就会被调用。

<input type="text" size="30" id="email" onchange="checkEmail()">

onSubmit

onSubmit 用于在提交表单之前验证所有的表单域。

下面是一个使用 onSubmit 事件的例子。当用户单击表单中的确认按钮时,checkForm() 函数就会被调用。假若域的值无效,此次提交就会被取消。checkForm() 函数的返回值是 true 或者 false。如果返回值为true,则提交表单,反之取消提交。

<form method="post" action="xxx.htm" onsubmit="return checkForm()">

onMouseOver onMouseOut

onMouseOver onMouseOut 用来创建动态的按钮。

下面是一个使用 onMouseOver 事件的例子。当 onMouseOver 事件被脚本侦测到时,就会弹出一个警告框:

<a href="http://www.w3school.com.cn"
onmouseover="alert('An onMouseOver event');return false">
 
<img src="w3school.gif" width="100" height="30">
 
</a>

 

异常捕捉

下一个例子会显示一个确认框,让用户来选择在发生错误时点击确定按钮来继续浏览网页,还是点击取消按钮来回到首页。如果 confirm 方法的返回值为 false,代码会把用户重定向到其他的页面。如果 confirm 方法的返回值为 true,那么代码什么也不会做。

<html>
<head>
<script type="text/javascript">
var txt=""
function message()
{
try
  {
  adddlert("Welcome guest!")   // alert误写为adddlert
  }
catch(err)
  {
  txt="There was an error on this page.\n\n"
  txt+="Click OK to continue viewing this page,\n"
  txt+="or Cancel to return to the home page.\n\n"
  if(!confirm(txt))
    {
    document.location.href="#"  //浏览器实现跳转
    }
  }
}
</script>
</head>
 
<body>
<input type="button" value="View message" onclick="message()" />
</body>
 
</html>

 

下面的实例的作用是测定变量 x 的值。如果 x 的值大于 10 或者小于 0,错误就会被抛出 (throw)。这个错误被 catch 的参数捕获后,就会显示出自定义的出错信息。

<html>
<body>
<script type="text/javascript">
var x=prompt("Enter a number between 0 and 10:","")
try
{ 
if(x>10) 
throw "Err1"
else if(x<0)
throw "Err2"
} 
catch(er)
{
if(er=="Err1") 
alert("Error! The value is too high")
if(er == "Err2") 
alert("Error! The value is too low") 
}
</script>
</body>
</html>

 

代码

输出

\'

单引号

\"

双引号

\&

和号

\\

反斜杠

\n

换行符

\r

回车符

\t

制表符

\b

退格符

\f

换页符

 

更多 JavaScript 对象

对象

描述

Window

JavaScript 层级中的顶层对象。Windows 对象会呈现 (represent) 一个浏览器窗口。每当 <body> 或者 <frameset> 标签出现,Window 对象就会被自动创建。

Navigator

包含客户端浏览器的信息。

Screen

包含客户端显示屏的信息。

History

包含了浏览器窗口访问过的 URL

Location

包含了当前URL的信息。

 

HTML DOM

对象

描述

Document

代表整个 HTML 文档,用来访问页面中的所有元素。

Anchor

代表 <a> 元素。

Area

代表图像地图中的 <area> 元素。

Base

代表 <base> 元素。

Body

代表图像地图中的 <body> 元素。

Button

代表 <button> 元素。

Event

代表事件的状态

Form

代表 <form> 元素

Frame

代表 <frame> 元素

Frameset

代表 <frameset> 元素

Iframe

代表 <iframe> 元素

Image

代表 <img> 元素

Input button

代表 HTML 表单中的按钮

Input checkbox

代表 HTML 表单中的选择框

Input file

代表 HTML 表单中的 fileupload

Input hidden

代表 HTML 表单中的隐藏域。

Input password

代表 HTML 表单中的密码域。

Input radio

代表 HTML 表单中的单选框。

Input reset

代表 HTML 表单中的重置按钮。

Input submit

代表 HTML 表单中的确认按钮。

Input text

代表 HTML 表单中的文本输入域。

Link

代表 <link> 元素

Meta

代表 <meta> 元素

Object

代表一个 <Object> 元素

Option

代表 <option> 元素

Select

代表 HTML 表单中的选择列表。

Style

代表某个单独的样式声明。

Table

代表 <table> 元素。

TableData

代表 <td> 元素。

TableRow

代表 <tr> 元素。

Textarea

代表 <textarea> 元素。

 

cookie

<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}
 
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
 
function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
  {alert('Welcome again '+username+'!')}
else 
  {
  username=prompt('Please enter your name:',"")
  if (username!=null && username!="")
    {
    setCookie('username',username,365)
    }
  }
}
</script>
</head>
 
<body onLoad="checkCookie()">
</body>
</html>

 

动画按钮效果

<html>
<head>
<script type="text/javascript">
function mouseOver()
  {
  document.b1.src ="/i/eg_mouse.jpg"
  }
function mouseOut()
  {
  document.b1.src ="/i/eg_mouse2.jpg"
  }
</script>
</head>
 
<body>
<a href="http://www.w3school.com.cn" target="_blank">
<img border="0" alt="Visit W3School!" src="/i/eg_mouse2.jpg" name="b1"
onmouseOver="mouseOver()"
onmouseOut="mouseOut()" />
</a>
</body>
</html>

 

JavaScript 图像地图

<html>
<head>
<script type="text/javascript">
function writeText(txt)
{
document.getElementById("desc").innerHTML=txt
}
</script>
</head>
 
<body>
<img src="planets.gif" width="145" height="126"
alt="Planets" usemap="#planetmap" />
 
<map id ="planetmap" name="planetmap">
<area shape ="rect" coords ="0,0,82,126"
onMouseOver="writeText('The Sun and the gas giant
planets like Jupiter are by far the largest objects
in our Solar System.')"
href ="sun.htm" target ="_blank" alt="Sun" />
 
<area shape ="circle" coords ="90,58,3"
onMouseOver="writeText('The planet Mercury is very
difficult to study from the Earth because it is
always so close to the Sun.')"
href ="mercur.htm" target ="_blank" alt="Mercury" />
 
<area shape ="circle" coords ="124,58,8"
onMouseOver="writeText('Until the 1960s, Venus was
often considered a twin sister to the Earth because
Venus is the nearest planet to us, and because the
two planets seem to share many characteristics.')"
href ="venus.htm" target ="_blank" alt="Venus" />
</map> 
 
<p id="desc"></p>
 
</body>
</html>

 

计时器

<html>
 
<head>
<script type="text/javascript">
var c=0
var t
 
function timedCount()
 {
 document.getElementById('txt').value=c
 c=c+1
 t=setTimeout("timedCount()",1000)
 }
 
function stopCount()
 {
 clearTimeout(t)
 }
</script>
</head>
 
<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
 
</html>

 

创建你自己的对象

有多种不同的办法来创建对象:

1. 创建对象的实例

下列代码创建了一个对象的实例,并向其添加了四个属性:

personObj=new Object()
personObj.firstname="John"
personObj.lastname="Doe"
personObj.age=50
personObj.eyecolor="blue"

personObj 添加方法也很简单。下列代码向 personObj 添加了名为 eat() 的方法:

personObj.eat=eat

2. 创建对象的模版

模版定义了对象的结构。

function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname
this.lastname=lastname
this.age=age
this.eyecolor=eyecolor
}

注意:模版仅仅是一个函数。你需要在函数内部向 this.propertiName 分配内容。

一旦拥有模版,你就可以创建新的实例,就像这样:

myFather=new person("John","Doe",50,"blue")
myMother=new person("Sally","Rally",48,"green")

同样可以向 person 对象添加某些方法。并且同样需要在模版内进行操作:

function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname
this.lastname=lastname
this.age=age
this.eyecolor=eyecolor
this.newlastname=newlastname
}

注意:方法只是依附于对象的函数而已。然后,我们需要编写 newlastname() 函数:

function newlastname(new_lastname)
{
this.lastname=new_lastname
}

Newlastname() 函数定义 person 的新的 lastname,并将之分配给 person。通过使用 “this.”JavaScript 即可得知你指的 person 是谁。因此,现在你可以这样写:myMother.newlastname("Doe")

 

posted @ 2008-10-06 20:05  Edward Xie  阅读(271)  评论(0编辑  收藏  举报