您的位置:首页 > Web前端 > JavaScript

javascript 学习笔记

2009-09-27 21:55 429 查看


(1)引入外部js文件:

<scriptsrc=”my.js”/>好像是这样呵呵,。

每个函数都默认有一个arguments对象来代表所有参数。

<scripttype="text/javascript">

functionadd(){

varout=0;

for(vari=0;i<arguments.length;i++){

out+=arguments[i];

}

returnout;}

</script>

<buttononclick="add(1,2,3,4,5)">Test</button>

(2)点击连接之后调用js代码

<ahref="javascript:addContact();">AddaContact</a>

点击图片之后改变图片

<imgsrc="button_off.gif"onmousedown="this.src='button_over.gif';">

onmousedown也可以为onmousedownonmouseoutonmouseup

点击按钮之后调用js

<buttononclick="doAction(multiply)">TestMultiply</button>


下面一段js代码说明了几个函数(功能是通过js新建一个按钮并带有各种属性)

<scripttype="text/javascript">

varboard=document.getElementById("board");//获得id号为board的元素,为div

vare4=document.createElement("input");//创建一个input即<input>

[b]e4.setAttribute("type","text");[/b]

//设置e4的type属性为text,即文本框这一句等于e.type=”text”

e4.setAttribute("name","q");//设置input的Name属性为q

e4.setAttribute("value","使用setAttribute");//input的初始值

e4.setAttribute("onclick","javascript:alert('Thisisatest!');");//设置点击事件

varobject=board.appendChild(e4);//将我们准备好的input放到<div>后面

</script>

<body><divid=board>

这句代码就等于<divid=board><inputname=qonclick=javascript:alert('Thisisatest!');>


最后来段简单的巩固一下,没啥好说的。

varimg=document.createElement('IMG');

img.setAttribute('src','delete.gif');

img.onclick=function(){

removeContact(tr);

}

td1.appendChild(img);

(3)getElementsByTagName:返回一个数组,包含具有相同tag名的所有元素类似于getElementById

(4)


注意看清楚了,这段代码分为4个子节点,其中第二个子节点又分3个子节点对了,我们现在要通过Js来修改内容,具体代码为


childNodes[1].childNodes[0]对应的应该就是<ahref=”link.html”>

childNodes[1].childNodes[1]对应的就是alink


(5)

setTimeout("alert('"+text+"');",1000);1秒之后弹出警告框

setInterval("alert('"+text+"');",1000);每隔1秒弹出一次警告框

varnewpasge=window.open('page2.html','TestWindow','width=500,height=200,resizable=yes');打开新窗口,newpasge为新窗口

window.close();关闭自己

改变状态栏:window.status(“****”);

前进后退window.history.go(1);

(6)js中的类详解见http://www.cnitblog.com/CoffeeCat/archive/2009/07/08/40138.html

Javascript中的类实现

(7)document对象

1:document.title="主页";修改网页标题

2:document.URL="1.html";直接在本网页中打开新网页。

3:网页中第一个有<imgsrc=”1.jpg”name=”myimg”/>

document.links[0]访问第一个<ahref=”1.html”>链接。

Document.images[0]或者document.image[“myimg”];代表第一个图片见上面

document.image[“myimg”].src

同理document.forms[0]或者document.forms[“myform1”];访问表单

4:document.write(“向网页中写入一段话”);

(8)location对象

location.href=“www.qq.com”;利用这种导航方式跳转页面,该页面会被放入历史页面中。

(9)navigator

navigator

(10)填充用户屏幕

Window.moveTo(0,0);

Window.resizeTo(screen.availWidth,screen.availHeight);//表示获取屏幕尺寸,将浏览器置于全屏
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: