一、搜狐、新浪、春夏秋冬练习

ps:获取name、id属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>document练习</title>
  6. <script type="text/javascript">
  7. //改变内容层
  8. function changeLink(){
  9. //获取div的id值同时清空内容
  10. var nodeVar = document.getElementById("node");
  11. nodeVar.innerHTML = "";
  12. nodeVar.innerHTML = "搜狐";
  13. }
  14. //清空p标签
  15. //所有input的value显示出来
  16. function all_input() {
  17. //获取input的所有属性
  18. var inputs = document.getElementsByTagName("input");
  19. //遍历出value 在 p标签显示
  20. var str = "";
  21. for(var i = 0; i<inputs.length; i++){
  22. str += inputs[i].value + "<br/>";
  23. }
  24. //p标签输出
  25. document.getElementById("s").innerHTML = str;
  26. }
  27. //显示season内容
  28. function s_input() {
  29. //获取 name = season
  30. var names = document.getElementsByName("season");
  31. //循环遍历出value
  32. var strName = "";
  33. for (var i = 0; i<names.length; i++){
  34. strName += names[i].value + "<br/>";
  35. }
  36. //p标签输出
  37. document.getElementById("s").innerHTML = strName;
  38. }
  39. </script>
  40. </head>
  41. <body>
  42. <div id="node">新浪</div>
  43. <input name="b1" type="button" value="改变层内容" onclick="changeLink();" /><br/>
  44. <br/><input name="season" type="text" value="春" />
  45. <input name="season" type="text" value="夏" />
  46. <input name="season" type="text" value="秋" />
  47. <input name="season" type="text" value="冬" />
  48. <br/><input name="b2" type="button" value="显示input内容" onclick="all_input();" />
  49. <input name="b3" type="button" value="显示season内容" onclick="s_input();" />
  50. <p id="s"></p>
  51. </body>
  52. </html>

二、选择爱好练习

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>选择你的爱好</title>
  6. <script type="text/javascript">
  7. //查看选择爱好
  8. function subButfun() {
  9. var valChecks = document.getElementsByName("valCheck");
  10. var str = "";
  11. //遍历选中的爱好
  12. for (var i=0; i<valChecks.length; i++){
  13. //判断是否选中了 --关键点
  14. if(valChecks[i].checked == true){
  15. str += valChecks[i].value + "<br/>";
  16. }
  17. }
  18. //p标签输出
  19. document.getElementById("showP").innerHTML = "";
  20. document.getElementById("showP").innerHTML = str;
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <h1>选择你的爱好</h1>
  26. <form action="#" method="post">
  27. <ul>
  28. <li><input type="checkbox" name="valCheck" value="音乐">音乐</li>
  29. <li><input type="checkbox" name="valCheck" value="登山">登山</li>
  30. <li><input type="checkbox" name="valCheck" value="游泳">游泳</li>
  31. <li><input type="checkbox" name="valCheck" value="阅读">阅读</li>
  32. <li><input type="checkbox" name="valCheck" value="打球">打球</li>
  33. <li><input type="checkbox" name="valCheck" value="跑步">跑步</li>
  34. <li><input type="checkbox" name="valCheck" value="其他">其他</li>
  35. </ul>
  36. </form>
  37. <hr/>
  38. <ul>
  39. <li><button id="subBut" onclick="subButfun()">查看我选择的爱好</button></li>
  40. </ul>
  41. <p id="showP"></p>
  42. </body>
  43. </html>