跟着杨中科学习asp.net之dom

Dom教程

使用javascript操作dom进行dhtml开发,目标:能够使用javascript操作dom实现常见的dhtml效果

Dom就是html页面的模型,将每个标签都做成为一个对象

,javascript通过调用dom中的属性、方法就可以对网页中的文本框、层等元素进行编程控制,比如通过操作文本框的dom对象,就可以读取文本框中的值、设置文本框中的值

Dom也像winform一样,通过事件、属性、方法进行编程

Javascript→dom就是c#→.net framework。

Css+javascritp+dom=dhtml

clip_image002

Dom事件

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

</head>

<body οnmοusedοwn="alert('别点我!')">

</body>

</html>

clip_image003

封装到函数里面:直接调用

<head>

<title></title>

<script type="text/javascript">

function bodymousedown()

{ alert('点到我了!'); alert('哈哈!') }

</script>

</head>

<body οnmοusedοwn="bodymousedown()">

</br></br></br></br></br></br></br></br></br></br></br></br>

</br></br></br></br></br></br></br></br></br></br></br></br>

</br></br></br></br></br></br>

</body>

</html>

clip_image004

<head>

<title></title>

<script type="text/javascript">

function bodymousedown()

{ alert('点到我了!'); alert('哈哈!') }

function f1() {

alert("我是f1");

}

function f2() {

alert("我是f2");

}

</script>

</head>

<!--<body οnmοusedοwn="bodymousedown()">-->

<input type="button" οnclick="document.οndblclick=f1" value="关联事件1" />

<input type="button" οnclick="document.οndblclick=f2" value="关联事件2" />

</br></br></br></br></br></br></br></br></br></br></br></br>

</br></br></br></br></br></br></br></br></br></br></br></br>

</br></br></br></br></br></br>

</body>

双击效果:

clip_image005

Window对象

clip_image006

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function confirmdemo() {

if (confirm("是否进入?")) {

alert("进入了!");

}

else {

alert("取消进入了!");

}

}

</script>

</head>

<body>

<input type="button" value="confirmtest" οnclick="confirmdemo()"/>

</body>

</html>

clip_image007

clip_image008

<input type="button" value="navigate测试" οnclick="navigate('HTMLPagedom.htm')"/>

重新导航到另外的页面

setInterval每隔一段时间执行指定的代码,第一个参数为代码

的字符串,第二个参数为间隔时间(单位毫秒),返回值为定时

器的标识

function startInterval() {

setInterval("alert('hello')", 5000)

}

</script>

</head>

<body>

<input type="button" value="confirmtest" οnclick="confirmdemo()"/>

<input type="button" value="navigate测试" οnclick="navigate('HTMLPagedom.htm')"/>

<input type="button" value="setInterval测试" οnclick="startInterval()"/>

clip_image009

clearInterval取消setInterval的定时执行,相当于Timer中的

Enabled=False。因为setInterval可以设定多个定时,所以

clearInterval要指定清除那个定时器的标识,即setInterval的返回

值。

var intervalId = setInterval("alert('hello')", 5000);

clearInterval(intervalId);

var intervalId;

function startInterval() {

intervalId=setInterval("alert('hello')", 5000)

}

</script>

</head>

<body>

<input type="button" value="confirmtest" οnclick="confirmdemo()"/>

<input type="button" value="navigate测试" οnclick="navigate('HTMLPagedom.htm')"/>

<input type="button" value="setInterval测试" οnclick="startInterval()"/>

<input type="button" value="停止Interval" οnclick="clearInterval(intervalId)"/>

</body>

clip_image011

setTimeout("alert('这是timeout')",2000)

走马灯

标题栏走马灯效果

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>新学期欢迎新同学</title>

<script type="text/javascript">

function scroll() {

var title = document.title;

}

</script>

</head>

<body>

</body>

</html>

clip_image012

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>新学期欢迎新同学</title>

<script type="text/javascript">

function scroll() {

var title = document.title;

var firstch = title.charAt(0);

var leftstr = title.substring(1,title.length);

document.title = leftstr + firstch;

}

scroll();

</script>

</head>

<body>

</body>

</html>

clip_image013

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>新学期欢迎新同学</title>

<script type="text/javascript">

function scroll() {

var title = document.title;

var firstch = title.charAt(0);

var leftstr = title.substring(1, title.length);

document.title = leftstr + firstch;

}

setInterval("scroll()", 500)

</script>

</head>

<body>

</body>

</html>

每隔500毫秒滚动,实现走马灯效果

练习:刚进入的时候还是向左滚动,点击【向左】按钮就向左连

续滚动,点击【向右】按钮就向右连续滚动。

• 思路1、全局变量,标志当前的滚动方向,当点击向左的时候

dir="left",向右dir="right"。

• 思路2、scrollleft scroolright,向右滚的时候将scrollleft的Interval

clear掉,然后setInterval启动scrollright

clip_image014

Dom事件

clip_image015

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script>

btn.value = "ok";

</script>

</head>

<body>

<input type="button" id="btn" value="模态对话框" οnclick="showModelessDialog('dialog.htm')"/>

</body>

</html>

clip_image016

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script>

// btn.value = "ok";

</script>

</head>

<body οnlοad="btn.value = 'ok'">

<input type="button" id="btn" value="模态对话框" οnclick="showModelessDialog('dialog.htm')"/>

</body>

</html>

clip_image017

在页面加载完成后onload才会调用

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script>

// btn.value = "ok";

</script>

</head>

<body οnlοad="btn.value = 'ok';" οnunlοad="alert('洛阳亲友如相问!')">

<input type="button" id="btn" value="模态对话框" οnclick="showModelessDialog('dialog.htm')"/>

</body>

</html>

clip_image019

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script>

// btn.value = "ok";

</script>

</head>

<body οnlοad="btn.value = 'ok';"

οnbefοreunlοad="window.event.returnValue=' 真的要放弃发帖退出吗?'">

<input type="button" id="btn" value="模态对话框" οnclick="showModelessDialog('dialog.htm')"/>

</body>

</html>

clip_image021

clip_image022

Dom属性

window对象的属性1

window.location.href='http://www.itcast.cn',重新导向新的地址,和navigate方

法效果一样。window.location.reload() 刷新页面

window.event是非常重要的属性,用来获得发生事件时的信息,事件不局限于

window对象的事件,所有元素的事件都可以通过event属性取到相关信息。类似

于winForm中的e(EventArg).

• altKey属性,bool类型,表示发生事件时alt键是否被按下,类似的还有

ctrlKey、shiftKey属性,例子 <input type="button" value="点击"

οnclick="if(event.altKey){alert('Alt点击')}else{alert('普通点击')}" /> ;

• clientX、clientY 发生事件时鼠标在客户区的坐标;screenX、screenY 发生

事件时鼠标在屏幕上的坐标;offsetX、offsetY 发生事件时鼠标相对于事件

源(比如点击按钮时触发onclick)的坐标。

• returnValue属性,如果将returnValue设置为false,就会取消默认事件的处

理。在超链接的onclick里面禁止访问href的页面。在表单校验的时候禁止提

交表单到服务器,防止错误数据提交给服务器、防止页面刷新。

<a href="http://www.baidu.com" οnclick="alert('禁止访问!')">百度</a>

点击百度后,页面会弹出禁止访问

clip_image024

但是点击ok后页面还是会跳转到百度

现要实现页面不跳转

要阻止跳转:

<a href="http://www.baidu.com" target="_blank" οnclick="alert('禁止访问!'); window.event.returnValue=false;">百度</a>

同样表单的提交操作类似:

<form action="Default.aspx">

<input type="submit" value="提交" οnclick="alert('数据异常禁止提交!');window.event.returnValue=false;"/>

</form>

• srcElement,获得事件源对象。几个事件共享一个事件响应函数用。

• keyCode,发生事件时的按键值。

• button,发生事件时鼠标按键,1为左键,2为右键,3为左右键同时按。

<body οnmοusedοwn="if(event.button==2){alert('禁止复制');}">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

</head>

<body>

<input type="button" value="href" οnclick="alert(location.href)"/>//显示当前页面的地址

</body>

</html>

clip_image025

<input type="button" value="点击"οnclick="if(event.altKey){alert('Alt点击')}else{alert('普通点击')}" />

clip_image026

定时器走马灯易错点:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>新学期欢迎新同学</title>

<script type="text/javascript">

function scrollleft() {

var title = document.title;

var lastch = title.charAt(title.length-1);//易错 返回最后一个字符charAt() 方法可返回指定位置的字符。

var leftstr = title.substring(0, title.length-1);//易错 substring截取字符串

document.title = lastch + leftstr;

}

// function scrollright() {

// var title = document.title;

// var firstch = title.charAt(title.length);

// var leftstr = title.substring(1, title.length);

// document.title = leftstr + firstch;

// }

// setInterval("scrollleft()", 500)

</script>

</head>

<body>

<input type="button" value="滚动" οnclick=" setInterval('scrollleft()', 500)">

</body>

</html>

每点击一次会启动一个定时器,因此滚动的速度会加快

停止代码写法:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>新学期欢迎新同学</title>

<script type="text/javascript">

function scrollleft() {

var title = document.title;

var lastch = title.charAt(title.length-1);//易错

var leftstr = title.substring(0, title.length-1);//易错

document.title = lastch + leftstr;

}

// function scrollright() {

// var title = document.title;

// var firstch = title.charAt(title.length);

// var leftstr = title.substring(1, title.length);

// document.title = leftstr + firstch;

// }

// setInterval("scrollleft()", 500)

</script>

</head>

<body>

<input type="button" value="滚动" οnclick="timeId=setInterval('scrollleft()', 500)">

<input type="button" value="停止定时器" οnclick=" clearInterval(timeId)">

</html>

Dom属性

clip_image027

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

</head>

<body>

<input type="button" value="分享本页给好友" οnclick="clipboardData.setData('Text','我发现这个页面很黄很暴力!'

+location.href);alert('已经将地址放到粘贴板中,赶快通知qq好友!');"/>

</body>

</html>

clip_image028

<body οncοpy="alert('禁止复制!');return false;">

<input type="button" value="分享本页给好友" οnclick="clipboardData.setData('Text','我发现这个页面很黄很暴力!'

+location.href);alert('已经将地址放到粘贴板中,赶快通知qq好友!');"/>abcdefg

clip_image029

所有元素都有oncopy,onpaste事件

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

</head>

<body οncοpy="alert('禁止复制!');return false;">

<input type="button" value="分享本页给好友" οnclick="clipboardData.setData('Text','我发现这个页面很黄很暴力!'

+location.href);alert('已经将地址放到粘贴板中,赶快通知qq好友!');"/>abcdefg

<br/>

<input type="text" οnpaste="alert('禁止粘贴!');return false;"/>

</body>

</html>

clip_image031

在网站中复制文章的时候,为了防止那些拷贝党不添加文章来源,

自动在复制的内容后添加版权声明。

function modifyClipboard() {

clipboardData.setData('Text', clipboardData.getData('Text')

+ '本文来自传智播客技术专区,转载请注明来源。' +

location.href);

}

οncοpy="setTimeout('modifyClipboard()',100)"。用户复制动作发生

0.1秒以后再去改粘贴板中的内容。100ms只是一个经常取值,

写1000、10、50、200……都行。不能直接在oncopy里修改粘

贴板。

不能直接在oncopy中执行对粘贴板的操作,因此设定定时器,0.1

秒以后执行,这样就不再oncopy的执行调用栈上了。

<script>

function modifyClipboard() {

clipboardData.setData('Text', clipboardData.getData('Text')

+ '本文来自传智播客技术专区,转载请注明来源。' +

location.href);

}

</script>

</head>

<body οncοpy="setTimeout('modifyClipboard()',100)">

<input type="button" value="分享本页给好友" οnclick="clipboardData.setData('Text','我发现这个页面很黄很暴力!'

+location.href);alert('已经将地址放到粘贴板中,赶快通知qq好友!');"/>abcdefg

<br/>

<input type="text" οnpaste="alert('禁止粘贴!');return false;"/>

</body>

</html>

前进后退导航

window对象的属性4

history操作历史记录

• window.history.back()后退;window.history.forward()前进。也可以

用window.history.go(-1)、window.history.go(1)前进

document属性。是最复杂的属性之一。后面讲解详细使用。

第一个页面:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<!--<javasctipt type="text/javascript">

function modifyClipboard() {

clipboardData.setData('Text', clipboardData.getData('Text')

+ '本文来自传智播客技术专区,转载请注明来源。' +

location.href);

}

</javascript>-->

<script>

function modifyClipboard() {

clipboardData.setData('Text', clipboardData.getData('Text')

+ '本文来自传智播客技术专区,转载请注明来源。' +

location.href);

}

</script>

</head>

<body οncοpy="setTimeout('modifyClipboard()',100)">

<input type="button" value="分享本页给好友" οnclick="clipboardData.setData('Text','我发现这个页面很黄很暴力!'

+location.href);alert('已经将地址放到粘贴板中,赶快通知qq好友!');"/>abcdefg

<br/>

<input type="text" οnpaste="alert('禁止粘贴!');return false;"/>

<input type="button" value="跳转页面" οnclick="navigate('HTMLPagehistory.htm')"/>

<input type="button" value="前进" οnclick="window.history.forward()">

</body>

</html>

第二个页面:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

</head>

<body>

</body>

<input type="button" value="后退" οnclick="window.history.back()"/>

</html>

或可写成<a href="javascript:window.history.back()">后退</a>

这样,效果是一样的

clip_image032

document属性。是最复杂的属性之一。后面讲解详细使用。

document属性1

document是window对象的一个属性,因为使用window对象成员

的时候可以省略window.,所以一般直接写document

document的方法:

• (1)write:向文档中写入内容。writeln,和write差不多,只不过

最后添加一个回车

• <input type="button" value="点击" οnclick="document.write('<font

color=red>你好</font>')" />

• 在onclick等事件中写的代码会冲掉页面中的内容,只有在页面加载

过程中write才会与原有内容融合在一起

• <script type="text/javascript">

• document.write('<font color=red>你好</font>');

• </script>

• write经常在广告代码、整合资源代码中被使用。见备注

内容联盟、广告代码、cnzz,不需要被主页面的站长去维护内容,只

要被嵌入的js内容提供商修改内容,显示的内容就变了。

1. write

<script type="text/javascript">

document.write("<a href='http://www.itcast.cn'>传智播客</a>");

</script>

clip_image033

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

</head>

<script type="text/javascript">

document.write("<a href='http://www.itcast.cn'>传智播客</a>");

</script>

<body>

<br/>

这是页面类容

<script type="text/javascript">

document.write("<a href='http://www.itcast.cn'>传智播客</a>");

</script>

这是页面类容

</body>

</html>

Script也可以写在body里面

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

</head>

<script type="text/javascript">

document.write("<a href='http://www.itcast.cn'>传智播客</a>");

</script>

<body>

<br/>

这是页面类容

<script type="text/javascript">

document.write("<a href='http://www.itcast.cn'>传智播客</a>");

</script>

<input type="button" value="hello" οnclick="document.write('hello')"/>

这是页面类容

</body>

</html>

clip_image034

点击hello按钮感觉会在新页面打印出来,但其实还是当前的页面只有在页面加载

过程中write才会与原有内容融合在一起(onlick页面已经加载完成)

clip_image035

广告联盟

http://news.baidu.com/newscode.html 百度广告获取广告代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<style type=text/css> div{font-size:12px;font-family:arial}.baidu{font-size:14px;line-height:24px;font-family:arial} a,a:link{color:#0000cc;}

.baidu span{color:#6f6f6f;font-size:12px} a.more{color:#008000;}a.blk{color:#000;font-weight:bold;}</style>

</head>

<script type="text/javascript">

document.write("<a href='http://www.itcast.cn'>传智播客</a>");

</script>

<body>

<br/>

这是页面类容

<script language="JavaScript" type="text/JavaScript" src="http://news.baidu.com/ns?word=title%3A%E8%AE%A1%E7%AE%97%E6%9C%BA&tn=newsfcu&from=news&cl=2&rn=5&ct=0"></script>

<script type="text/javascript">

document.write("<a href='http://www.itcast.cn'>传智播客</a>");

</script>

<input type="button" value="hello" οnclick="document.write('hello')"/>

这是页面类容

</body>

</html>

clip_image037

可以直接在记事本里面打开别人网站的js代码

clip_image038

http://news.baidu.com/ns?word=title%3A%E8%AE%A1%E7%AE%97%E6%9C%BA&tn=newsfcu&from=news&cl=2&rn=5&ct=0

clip_image040

Cnzz数据专家

document方法

getElementById方法(非常常用),根据元素的Id获得对象,网页中id不能

重复。也可以直接通过元素的id来引用元素,但是有有效范围、

form1.textbox1之类的问题,因此不建议直接通过id操作元素,而是通过

getElementById

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function btnClick() {

alert(textbox1.value);

}

</script>

</head>

<body>

<input type="text" id="textbox1" />

<input type="button" value="点一下" οnclick="btnClick()"/>

</body>

</html>

通过控件的id取value值但不推荐这样使用textbox1.value会出现无法取到的情况

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function btnClick() {

alert(textbox1.value);

}

function btnClick2() {

alert(textbox2.value);

}

</script>

</head>

<body>

<input type="text" id="textbox1" />

<input type="button" value="点一下" οnclick="btnClick()"/>

<form action="Default.aspx">

<input type="text" id="textbox2" />

<input type="button" value="点一下" οnclick="btnClick2()"/>

</form>

</body>

</html>

clip_image042

错误原因:input放在了form中,引用时不能直接引用input的id

正确用法:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function btnClick() {

alert(textbox1.value);

}

function btnClick2() {

// alert(textbox2.value);

alert(form1.textbox2.value);

}

</script>

</head>

<body>

<input type="text" id="textbox1" />

<input type="button" value="点一下" οnclick="btnClick()"/>

<form action="Default.aspx" id="form1">

<input type="text" id="textbox2" />

<input type="button" value="点一下" οnclick="btnClick2()"/>

</form>

</body>

</html>

clip_image043

这样使用每次要找到form的id,再通过这个id来找控件的id略显麻烦,因此推荐使用另外一种方法:getElementById(‘控件名’)

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function btnClick() {

var txt = document.getElementById("textbox1");

alert(txt.value);

// alert(textbox1.value);

}

function btnClick2() {

// alert(textbox2.value);

// alert(form1.textbox2.value);

var txt = document.getElementById("textbox2");

alert(txt.value);

}

</script>

</head>

<body>

<input type="text" id="textbox1" />

<input type="button" value="点一下" οnclick="btnClick()"/>

<form action="Default.aspx" id="form1">

<input type="text" id="textbox2" />

<input type="button" value="点一下" οnclick="btnClick2()"/>

</form>

</body>

</html>

(*)getElementsByName,根据元素的name获得对象,由于页面中元素

的name可以重复,比如多个RadioButton的name一样,因此

getElementsByName返回值是对象数组。

易错点:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function btnClick() {

var radios=document.getElementsByName("gender");

for(var r in radios)

{

alert(r.value);

}

}

</script>

</head>

<body>

<input type="radio" value="男" name="gender"/>男

<input type="radio" value="女" name="gender"/>女

<input type="radio" value="未知" name="gender"/>未知

<input type="button" value="click" οnclick="btnClick()"/>

</body>

</html>

clip_image044

正确方法:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function btnClick() {

var radios = document.getElementsByName("gender");

// for(var r in radios)

// {

// alert(r.value);

// }

// }在js中不像C#中的foreach,并不会遍历每个元素,而是遍历的key

for (var i = 0; i < radios.length; i++) {

var radio = radios[i];

alert(radio.value);

}

}

</script>

</head>

<body>

<input type="radio" value="男" name="gender"/>男

<input type="radio" value="女" name="gender"/>女

<input type="radio" value="未知" name="gender"/>未知

<input type="button" value="click" οnclick="btnClick()"/>

</body>

</html>

(*)getElementsByTagName,获得指定标签名称的元素数组,比如

getElementsByTagName("p")可以获得所有的<p>标签。

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function btnClick() {

var radios = document.getElementsByName("gender");

// for(var r in radios)

// {

// alert(r.value);

// }

// }在js中不像C#中的foreach,并不会遍历每个元素,而是遍历的key

for (var i = 0; i < radios.length; i++) {

var radio = radios[i];

alert(radio.value);

}

}

function btnClick2() {

var inputs = document.getElementsByTagName("input");

for (var i = 0; i < inputs.length; i++) {

var input = inputs[i];

input.value = "hello";

}

}

</script>

</head>

<body>

<input type="radio" value="男" name="gender"/>男

<input type="radio" value="女" name="gender"/>女

<input type="radio" value="未知" name="gender"/>未知

<input type="button" value="click" οnclick="btnClick()"/>

<input type="text"/>

<input type="text"/>

<input type="text"/>

<input type="text"/>

<input type="text"/>

<input type="button" value="bytagname" οnclick="btnClick2()"/>

</body>

</html>

clip_image046

案例:点击一个按钮,被点击的按钮显示“呜呜”,其他按钮显示“哈哈”。

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function initEvent() {

var inputs = document.getElementsByTagName("input");

for (var i = 0; i < inputs.length;i++ ) {

var input = inputs[i];

input.onclick = btnClick;

}

}

function btnClick() {

alert("点了!");

}

</script>

</head>

<body οnlοad="initEvent()">

<input type="button" value="哈哈"/>

<input type="button" value="哈哈"/>

<input type="button" value="哈哈"/>

<input type="button" value="哈哈"/>

<input type="button" value="哈哈"/>

</body>

</html>

clip_image047

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function initEvent() {

var inputs = document.getElementsByTagName("input");

for (var i = 0; i < inputs.length;i++ ) {

var input = inputs[i];

// input.onclick = btnClick;

// window.event.srcElement,获得引发事件的控件

input.onclick = btnClick;

}

}

function btnClick() {

var inputs = document.getElementsByTagName("input");

for (var i = 0; i < inputs.length; i++) {

var input = inputs[i];

if (input == window.event.srcElement) {

input.value = "呜呜";

}

else {

input.value = "哈哈";

}

}

}

// function btnClick() {

// alert("点了!");

// }

</script>

</head>

<body οnlοad="initEvent()">

<input type="button" value="哈哈"/>

<input type="button" value="哈哈"/>

<input type="button" value="哈哈"/>

<input type="button" value="哈哈"/>

<input type="button" value="哈哈"/>

</body>

</html>

案例:十秒钟后协议文本框下的注册按钮才能点击,时钟倒数。

(btn.disabled = true )

思路:1.注册按钮初始状态为不可用

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>注册</title>

<script type="text/javascript">

</script>

</head>

<body>

<textarea></textarea>

<input type="button" value="同意" disabled="disabled"/>

</body>

</html>

2. 启动定时器setInterval,1秒钟运行一次CountDown的方法,设定初始值10的全局变量,在

countDonw方法中对全局变量倒数

3. 将倒数的值写到按钮上(请仔细阅读协议(还剩8秒))

4. 直到全局变量的值<=0时按钮可用,并且设定按钮上文本为同意

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>注册</title>

<script type="text/javascript">

var leftScconds = 10;

var intervalId;

function CountDown() {

var btnReg = document.getElementById("btnReg");

if(btnReg)//如果网页的加载速度非常慢的话,可能定时器运行的时候控件还没有加载!

{

if (leftScconds <= 0) {

btnReg.value = "同意";

btnReg.disabled = ""; //或者btnReg.disabled=false;

clearInterval(intervalId);//停止计时器

}

else {

btnReg.value = "请仔细阅读协议(还剩" + leftScconds + "秒)";

leftScconds--;

}

}

}

intervalId=setInterval("CountDown()", 1000);

</script>

</head>

<body>

<textarea></textarea>

<input type="button" value="同意" disabled="disabled" id="btnReg"/ >

</body>

</html>

clip_image048

clip_image049

练习:加法计算器。两个文本框中输入数字,点击【=】按钮将相加的结果

放到第三个文本框中。

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function calClick() {

var value1 = document.getElementById("text1").value;

var value2 = document.getElementById("text2").value;

document.getElementById("text3").value = value1 + value2;

}

</script>

</head>

<body>

<input type="text" id="text1"/>+<input type="text" id="text2"/>=

<input type="button" οnclick="calClick()" value="="/><input type="text" id="text3" readonly="readonly"/>

</body>

</html>

执行的结果为拼接字符串

正确的做法为:(转换为int类型)

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function calClick() {

var value1 = document.getElementById("text1").value;

var value2 = document.getElementById("text2").value;

value1 = parseInt(value1, 10);

value2= parseInt(value2, 10);

document.getElementById("text3").value = value1 + value2;

}

</script>

</head>

<body>

<input type="text" id="text1"/>+<input type="text" id="text2"/>=

<input type="button" οnclick="calClick()" value="="/><input type="text" id="text3" readonly="readonly"/>

</body>

</html>

clip_image051

注意parseInt(参数一,参数二)

参数一为要转换的数,参数二为结果的进制数(这里是十进制,所以就是10)

练习:美女时钟。

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function ReFlash() {

var imgMM = document.getElementById("imgMM");

if (!imgMM) {

return;

}

else {

imgMM.src = "Images/1.jpg";

}

}

setInterval("ReFlash()",1000);

</script>

</head>

<body>

<img id="imgMM" src=""/>

</body>

</html>

clip_image053

完整代码如下:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function ReFlash() {

var imgMM = document.getElementById("imgMM");

if (!imgMM) {

return;

}

// else {

// imgMM.src = "Images/1.jpg";

// }

var now = new Date();

var filename = now.getHours() + "_" + now.getSeconds() + ".jpg";

imgMM.src = "mm/" + filename;

}

setInterval("ReFlash()",1000);

</script>

</head>

<body>

<img id="imgMM" src=""/>

</body>

</html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function Fill2Len(i) {

if (i < 10) {

return "0" + i;

}

else {

return i;

}

}

function ReFlash() {

var imgMM = document.getElementById("imgMM");

if (!imgMM) {

return;

}

// else {

// imgMM.src = "Images/1.jpg";

// }

var now = new Date();

var filename = Fill2Len(now.getHours()) + "_" + Fill2Len(now.getSeconds()) + ".jpg";

imgMM.src = "mm/" + filename;

}

setInterval("ReFlash()",1000);

</script>

</head>

<body οnlοad="ReFlash()">

<img id="imgMM" src=""/>

</body>

</html>

clip_image055

转载于:https://www.cnblogs.com/seclusively/p/3789487.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值