JS学习(二)

1 数组对象

作用是:使用单独的变量名来存储一系列的值。

1.1 新建数组:

var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"

1.2 For In声明

for (x in mycars)
{
document.write(mycars[x] + "<br />")
}

1.3 合并数组

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

var arr2 = new Array(3)
arr2[0] = "James"
arr2[1] = "Adrew"
arr2[2] = "Martin"

var arr3 = arr.concat(arr2)

document.write(arr3)

输出:George,John,Thomas,James,Adrew,Martin

1.4 生成字符串join

var str = arr.join()

George,John,Thomas

var str = arr.join(".")

George.John.Thomas

1.5 数组排序sort

function sortNumber(a, b)
{
return a<b
}
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"

document.write(arr + "<br />")
document.write(arr.sort(sortNumber))

arr.sort()默认升序排序

2 消息框

2.1 警示框Alert

alert("再次向您问好!在这里,我们向您演示" + '\n' + "如何向警告框添加折行。")

2.2 确认框comfirm

confirm("Press a button!");

2.3 提示框prompt

prompt("请输入您的名字","Bill Gates")

3 cookie

3.1 创建和存储 cookie

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())

}

3.2 获取cookie

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 ""

}

你可能感兴趣的:(js)