整理的javascript一些常用的方法(一)
1,脚本的执行:
(1)打开页面时执行脚本。
浏览器打开一个html文档时,会解释整个文档,包括html标签和脚本,如果脚本中有可以直接
执行的语句,则会在遇到的时候马上执行。
(2)利用onload事件执行脚本。
onload事件是在一个页面在浏览器中打开时发生。可在<body>标签中调用onload事件。
(3)通过用户事件执行脚本。
用户事件是指移动鼠标,点击连接,单击按钮等行为。
2,javascript基本数据类型:
数字型(整数和实数),字符串型,逻辑型(true或false),空值。
3,window(窗口)对象:
(1)对象的属性:name,length,parent,self,top,status,default status,opener,
closed。
(2)open()方法:打开一个新的浏览器窗口,打开新窗口的参数通过url传递给open()。
function opennew()
{
window.open("new.html",null,"height=300,width=300,
status=yes,toolbar=no,menuba=no.location=no")
}
(3)confirm()方法:显示一个可以接受用户输入的确认框,当用户单击【ok】或【cancel】我
们可以检测到哪个动作发生了。
<body>
<form name=conform>
<center>
<input type="button" value="单击" onClick="conwin()">
<input type="text" name=box1 size=20>
</form>
</body>
<script language=JavaScript>
function conwin(){
if(confirm("单击【ok】按钮或者【cancel】按钮。。。"))
{
document.conform.box1.value="【ok】按钮被单击!"
}
else{
document.conform.box1.value="【cancel】按钮被单击!"
}
}
</script>
(4)show Modal Dialog()方法:通过dialog创建新的窗口,
(5)focus()方法:windows.focus()方法获取焦点位置。
反向操作方法windows.blur()方法
(6)创建窗口:
windowVar=window.open(url,windowName,windowFeature)
url:指向的一个目标窗口。
windowName:创建窗口对象是名字。
windowFeature:
4.document对象:
(1)对象属性:alinkcolor,bgcolor,cookie,domain,embeds,fgcolor,o,ages,layers,
linkecolor,location,title,url,vlinkcolor
(2)cookie属性:包含客户机的状态信息(一段信息字符串值)。
获取cookie:function getcookie(name)
{
var result=null;
var mycookie=""+document.cookie+";";
var searchName=""+name+"=";
var startcookie=mycookie.indexof(searchName);
var endcookie;
if(startcookie!=-1)
{
startcookie+=searchName.length;
endcookie=mycookie,indexof(";",startcookie);
result=unescape(mycookie.substring(startcookie,endcookie));
}
return result;
}
(3)forms属性:可以使用document.forms属性来定位表单对象或者元素。
例如:获取文档中的第三个表单的名称为“testphone”的输入文本域的值:
myphone=document.forms[2].testphone.value
5,history对象:
属性:current,length,next,previous
主要方法:back(),forward(),go()等。
6,字符串对象:
(1)charat():该方法从字符串中返回一个字符,应用时通常会设置一个起始位置的参数,然后传
回位于该字符串对象位置的字符串。系统认为起始从0开始。
string.charAt(index)
(2)indexOf():从一个特定的位置开始查找设置的字符,返回一个字符或是字符串上起始位置值,如
果特定的位置找不到用户设置的字符串对象,就返回一个-1.
string.indexOf(string,index)
比如:
s="你中奖了!"
s2="中奖"
s3=s.indexOf(s2)
(3)lastindexOf():与indexOf()方法一样,但是该方法从字符串对象的尾部开始搜索。
(4)substring:将两个参数间的字符串返回给设置的变量。
string.substring(index1,index2)
(5)toLowerCase:将字符串对象中的所有字符转换成小写的字符
string.toLowerCase()
(6)toUpperCase():小写转大写
string.toUpperCase()
(7)anchor():在页面中创建和显示一个html超文本目标。
7,日期对象:setYear,setTime,getTime,getDay,getMonth,getYear等
thisday=new Date()或者thisday=new Date(month day,year hours:minutes:seconds)
getMoth:返回月份
getDate:返回一个月份中的日期
getDay:返回星期数
(1)toGMTString():将一个日期转换为一个字符串
(2)toLocaletSring():将日期转换为本地的日期格式。
date.toLocaletSring()
8,数学函数
(1)random():产生一个位于0和1之间的随机数。
Math.random()
9,数组对象
(1)创建格式:
function students(name,age)
{
this.name=name;
this.age=age;
}
创建之后就可以创建实例:stu1=new student("白云",30);
(2)对象类数组:stu=new Array(5)
stu[0]="小白"