js_day26--js DOM编程(forms/form对象+table对象)
来源:互联网 发布:淘宝店铺客服怎么登陆 编辑:程序博客网 时间:2024/05/18 02:54
Day26
案例1:简易购物车
<!DOCTYPE html><html> <head> <title>mycart.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--><script type="text/javascript" language="javascript"><!--function buy(obj){//遍历所有的checkbox,计算新的总价var fruits=document.getElementsByName("fruit");var totalPrice = 0;for(var i=0;i<fruits.length;i++){//判断是否选择if(fruits[i].checked){totalPrice+=parseFloat(fruits[i].value);}}myspan.innerText=totalPrice+"";}//--></script> </head> <body> <h1>我的购物车</h1> <input type="checkbox" name="fruit" value="10" onclick="buy(this)"/>苹果 10元 <br/> <input type="checkbox" name="fruit" value="20" onclick="buy(this)"/>香蕉 20元<br/> <input type="checkbox" name="fruit" value="30" onclick="buy(this)"/>西瓜 30元<br/> <input type="checkbox" name="fruit" value="40" onclick="buy(this)"/>梨子 40元<br/> <input type="checkbox" name="fruit" value="50" onclick="buy(this)"/>哈密瓜 50元<br/> 总价格是:<span id="myspan">0</span>元 </body></html>
案例2:全选与取消全选
<!DOCTYPE html><html> <head> <title>selectCheckBox.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--><script type="text/javascript"><!--var fruits = document.getElementsByName("fruit");function selectCheckBox(obj){//window.alert(obj.innerText);if(obj.innerText=="全选"){for(var i=0;i<fruits.length;i++){fruits[i].checked=true;}}else if(obj.innerText=="取消"){for(var i=0;i<fruits.length;i++){fruits[i].checked=false;}}}//响应复选框function selectCheckBox2(){if(myselect.checked){for(var i=0;i<fruits.length;i++){fruits[i].checked=true;}}else{for(var i=0;i<fruits.length;i++){fruits[i].checked=false;}}}//--></script> </head> <body> <input type="checkbox" name="fruit" value="10" onclick="buy(this)"/>苹果 10元 <br/> <input type="checkbox" name="fruit" value="20" onclick="buy(this)"/>香蕉 20元<br/> <input type="checkbox" name="fruit" value="30" onclick="buy(this)"/>西瓜 30元<br/> <input type="checkbox" name="fruit" value="40" onclick="buy(this)"/>梨子 40元<br/> <input type="checkbox" name="fruit" value="50" onclick="buy(this)"/>哈密瓜 50元<br/> <a href="#" onclick="selectCheckBox(this)">全选</a> <a href="#" onclick="selectCheckBox(this)">取消</a> <input type="checkbox" id="myselect" onclick="selectCheckBox2()"/>是否全选 </body></html>
forms对象(集合)定义:
按照表单在文档中的顺序得到form对象,forms对象集合中包括了当前文档的所有form对象。
讲forms对象(集合)的目的其实是为了讲解form对象。
length: 设置或得到集合的大小
forms[1]等价于forms.item(1)
当访问某个表单的某个元素的时候,可以使用上面两种方法。
案例3:form对象
<!DOCTYPE html><html> <head> <title>forms.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--><script type="text/javascript"><!--function test(){var allform = document.forms;//拿到所有的表单//window.alert(allform.length);//可以通过allform访问指定表单window.alert(allform[1].hobby2.value);window.alert(allform.item(1).hobby1.value);}//--></script> </head> <body> <h1>个人信息</h1> <form action=""> u:<input type="text" name="username"/><br> p:<input type="password" name="pwd"/><br> <input type="submit" value="提交"/> </form> <h1>兴趣爱好</h1> <form action=""> 爱好1:<input type="text" name="hobby1"/><br> 爱好2:<input type="text" name="hobby2"/><br> <input type="submit" value="提交"/> </form> <input type="button" value="testing" onclick="test()"/> </body></html>
在验证表单的时候,可以在
<form action=”” onsubmit=”函数”/>
也可以在
<input type=”submit” onclick=”函数”/>
写一个就可以了,没有必要多写,否则验证两次,不划算!
images对象(集合)/img对象
imagesàimg对象
onload 图片在加载成功后调用
onerror图片在加载失败后调用
links对象(集合)定义:
links—>link对象,与前面类似
table对象
鉴于table对象在web编程中的重要性,这里重点强调table对象。
table对象代表一个html表格。
在HTML文档中<table>标签每出现一次,一个table对象就会被创建。
table对象的例子参照window对象3中的猜拳游戏!
0 0
- js_day26--js DOM编程(forms/form对象+table对象)
- JS:DOM相关:table对象,节点操作
- JS DOM编程 window对象
- forms对象集合和form对象
- DOM的Form对象
- HTML DOM Table 对象
- DOM之Form对象(一)
- js_day20--js DOM编程(window对象1)
- js_day21--js DOM编程(window对象2)
- js_day22--js DOM编程(window对象3+猜拳游戏)
- js_day24--js DOM编程(document+body对象)
- Dom编程(Window 对象)
- DHTML DOM的Form对象
- js form对象[转载]
- js基础-form对象
- js的dom对象
- js操作dom对象
- js-访问DOM对象
- 第五届蓝桥杯试题——绳圈
- hdu 4699 模拟栈
- AndroidSQLite3使用小例(超棒)
- Android使用静态变量传递数据
- 解放思想,Web打印终极解决方案
- js_day26--js DOM编程(forms/form对象+table对象)
- 使用花生壳ADO控件互联网远程连接到服务器
- python dict 两种遍历方式的比较
- OOP Essentials - Thinking in Java
- bnuoj 1065 简单的问题(位运算)
- IOS实现UIImage倒影的三种方法
- 总结
- PAT: 1023. Have Fun with Numbers (20)
- 循环队列