一、搜狐、新浪、春夏秋冬练习
ps:获取name、id属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>document练习</title>
<script type="text/javascript">
//改变内容层
function changeLink(){
//获取div的id值同时清空内容
var nodeVar = document.getElementById("node");
nodeVar.innerHTML = "";
nodeVar.innerHTML = "搜狐";
}
//清空p标签
//所有input的value显示出来
function all_input() {
//获取input的所有属性
var inputs = document.getElementsByTagName("input");
//遍历出value 在 p标签显示
var str = "";
for(var i = 0; i<inputs.length; i++){
str += inputs[i].value + "<br/>";
}
//p标签输出
document.getElementById("s").innerHTML = str;
}
//显示season内容
function s_input() {
//获取 name = season
var names = document.getElementsByName("season");
//循环遍历出value
var strName = "";
for (var i = 0; i<names.length; i++){
strName += names[i].value + "<br/>";
}
//p标签输出
document.getElementById("s").innerHTML = strName;
}
</script>
</head>
<body>
<div id="node">新浪</div>
<input name="b1" type="button" value="改变层内容" onclick="changeLink();" /><br/>
<br/><input name="season" type="text" value="春" />
<input name="season" type="text" value="夏" />
<input name="season" type="text" value="秋" />
<input name="season" type="text" value="冬" />
<br/><input name="b2" type="button" value="显示input内容" onclick="all_input();" />
<input name="b3" type="button" value="显示season内容" onclick="s_input();" />
<p id="s"></p>
</body>
</html>
二、选择爱好练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>选择你的爱好</title>
<script type="text/javascript">
//查看选择爱好
function subButfun() {
var valChecks = document.getElementsByName("valCheck");
var str = "";
//遍历选中的爱好
for (var i=0; i<valChecks.length; i++){
//判断是否选中了 --关键点
if(valChecks[i].checked == true){
str += valChecks[i].value + "<br/>";
}
}
//p标签输出
document.getElementById("showP").innerHTML = "";
document.getElementById("showP").innerHTML = str;
}
</script>
</head>
<body>
<h1>选择你的爱好</h1>
<form action="#" method="post">
<ul>
<li><input type="checkbox" name="valCheck" value="音乐">音乐</li>
<li><input type="checkbox" name="valCheck" value="登山">登山</li>
<li><input type="checkbox" name="valCheck" value="游泳">游泳</li>
<li><input type="checkbox" name="valCheck" value="阅读">阅读</li>
<li><input type="checkbox" name="valCheck" value="打球">打球</li>
<li><input type="checkbox" name="valCheck" value="跑步">跑步</li>
<li><input type="checkbox" name="valCheck" value="其他">其他</li>
</ul>
</form>
<hr/>
<ul>
<li><button id="subBut" onclick="subButfun()">查看我选择的爱好</button></li>
</ul>
<p id="showP"></p>
</body>
</html>