javaScript 进阶篇

1.js 数组

创建数组的语法:

a. var myarray= new Array(8);

myarray[0]=1;等等

b.var myarray = new Array(66,8,47,59,43);

c.var myarray= [50,3,6,9,78,45];

注意:a.创建的新数组是空数组,没有值,如输出,则显示undefined.

b.虽然创建数组时,指定了长度,但实际上数组都是变长的,也就是说即使指定了长度为8,仍然可以将元素存储在规定长度以为。

c.数组每个值都有索引号,从0开始;

d.数组存储的数据可以是任何类型(数字、布尔值、字符等)

2.向数组增加新元素

例如:var  myarr =[1,2,3,4,5,];

myarr数组有5个元素,角标最大为4.

可以直接添加 myarr[20]=15;

这时查询数组的长度为21;

3.获取数组的长度;

数组名.length;

例如:

var arr =[1,2,3,6,5,8,9,4,7];

document.write(arr.length);

注意:数组的长度是可变的;

arr.length=10;

document.write(arr.length);//这时数组的长度为10.

4.二维数组

myarr [][];

二维数组的定义方法:var myarr=[[1,2,3,7],[5,6,8,1,3]];

二维数组的赋值; myarr[0][1]=20;//数组中0表示表的行为0,1表示表的列为1;

5.javaScript 中也有循环语句

if 语句:

var res =confirm("你喜欢美女");

 if(res == true){

document.write('喜欢')}

if ...else 语句

var res =confirm("你喜欢美女");

 if(res == true){

document.write('喜欢')

 }else{

 document.write("不喜欢");

 }

还有:swich语句、for循环、while 循环、do..while 循环、break、continue(用法同java一样)

 注意:javaScript 中“==”号可以判断a="25"; b=25;a和b的相等

       javaScript 中“===”号要判断两个变量是否相等必须要a和b 的类型一样值一样才行;

6.鼠标单击事件(onclick)

onclick 是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件。同时onclick事件调用的程序就会被执行。

例如:

<script type="text/javascript">

function f_open (){

mywin= window.open('https://www.baidu.com');

}

function f_close(){

mywin.close();

}

</script>

<body>

<input type="button"  id="" value="点击打开窗口" onclick="f_open()"/>

<input type="button"  id="" value="点击关闭窗口" onclick="f_close()"/>

</body>

注意:在网页中,如何使用事件,就在该元素中设置事件属性。

7.鼠标经过事件(onmouseover)鼠标移开事件(onmouseout)

鼠标经过事件,当鼠标移到一个对象上时,该对象就触发 onmouseover 事件,并执行 onmouseover事件调用的程序。

鼠标移开事件,当鼠标移开当前对象时,执行onmouseout 调用的程序。

<body>

<p id="pp" onmouseover="on_mov() "onmouseout="on_mou()">aaaa</p>

</body>

<script type="text/javascript">

function on_mov(){//鼠标经过事件

document.getElementById("pp").style.color="red";

}

function on_mou(){//鼠标离开事件

document.getElementById("pp").style.color="black";

}

</script>

8.光标的聚焦事件(onfocus)和光标的失焦事件(onblur)

<body>

<input type="text" name="username" id="username" value="请输入姓名" onfocus="on_focus() " onblur="on_blur()"/>

</body>

<script type="text/javascript">

function on_focus(){//光标聚焦后的参数

alert("请输入姓名:");

}

function on_blur(){//光标失焦后的参数

confirm("不要离开");

}

</script> 

9.内容选中事件(onselect)

<input type="text" name="" id="" value="hello world" onselect="xuanz()"/>

<script type="text/javascript">

function xuanz(){

alert("你出发了选中事件");

}

</script> 

10.文本框内容改变事件(onchange)

<input type="text" name="" id="" value="hello world" onchange="change()" />

<script type="text/javascript">

function  change(){

alert("你改变了文本内容!");

}

</script>

原文地址:https://www.cnblogs.com/sbj-dawn/p/7029162.html