{welcome to JS} 使用多级条件(switch&case)
2015-09-17 12:24
531 查看
welcome09.html
<!DOCTYPE html> <html> <head> <title>621#</title> <script src = "script09.js"></script> </head> <body> <h2>About these girls</h2> <form action = "#"> <input type = "button" id = "ChenDan" value = "ChenDan"> <input type = "button" id = "ManXingchen" value = "ManXingchen"> <input type = "button" id = "HuangYan" value = "HuangYan"> <input type = "button" id = "LiangCan" value = "LiangCan"> <input type = "button" id = "ZengXiao" value = "ZengXiao"> <input type = "button" id = "Lingle" value = "Lingle"> </form> </body> </html>
script09.js
window.onload = initAll; function initAll(){ document.getElementById("ChenDan").onclick = saySomething; document.getElementById("ManXingchen").onclick = saySomething; document.getElementById("HuangYan").onclick = saySomething; document.getElementById("LiangCan").onclick = saySomething; document.getElementById("ZengXiao").onclick = saySomething; document.getElementById("Lingle").onclick = saySomething; } function saySomething(){ switch(this.id){ case "ChenDan": alert("She is our dorm head."); break; case "ManXingchen": alert("We like to play with her."); break; case "HuangYan": alert("She is from Fu Jian"); break; case "LiangCan": alert("She comes from Gui Zhou"); break; case "ZengXiao": alert("Her English is very good."); break; case "Lingle": alert("hhh,I don't know."); break; default: } }
运行结果
学习心得
因为在HTML中设置了id属性和value属性,可用getElementById(),也可用getElementByValue()。<form> 标签用于为用户输入创建 HTML 表单。
表单能够包含 input 元素,比如文本字段、复选框、单选框、提交按钮等等。
相关文章推荐
- JSP程序运行原理、文档结构及简单输入输出实例分析
- Javascript 中英文字符串截取
- Jsonp post 跨域方案
- JS-实现瀑布流
- js之事件冒泡和事件捕获详细介绍
- js,css 和 html 分离,见仁见智
- {welcome to JS} 无干扰脚本编程
- js的MVC结构设计
- js学习笔记
- 关于Eclipse中JSP,JS,HTML等代码自动提示的设置
- JS最常用 正则表达式3
- JS最常用 正则表达式2
- js-linq.js 从json字符串中按条件查找数据
- JavaScript:delete的用法
- JS如何判断有没有上一页?
- 数据解析2:JSON解析(1)
- javascript 基础学习教程
- 判断js数据类型
- JS+CSS实现精美的二级导航效果代码
- JavaScript之数据类型