鱼C论坛

 找回密码
 立即注册
查看: 1925|回复: 10

初学html,css,js,作品口算系统

[复制链接]
发表于 2020-4-16 11:22:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
index.html


                               
登录/注册后可看大图

  1. <!DOCTYPE html>
  2. <html>
  3.         <!-- 文档的头部,定义了一些属性 -->
  4.         <head>
  5.                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6.                 <!-- 标签标题 -->
  7.                 <title>在线口算</title>
  8.                 <link rel="stylesheet" type="text/css" href="page.css">
  9.                 <script type="text/javascript" src="function.js"></script>
  10.         </head>
  11.         <body>
  12.                 <div id="wrapper">
  13.                         <div id="content">
  14.                                 <h1>在线口算练习系统<img src="by.png" style="float: right; width:29.5px; height: 41.3px ;"></h1>
  15.                                 <div id="head">
  16.                                         <br />
  17.                                         <!--选择数字范围输入框-->
  18.                                         最小数字:<input id="min" value='1'>
  19.                                         最大数字:<input id="max" value='10'>

  20.                                         <!--试题数量选择-->
  21.                                         试题数量:<input type="radio" name="maxti" value="20" class="radioclass" checked>20
  22.                                         <input type="radio" name="maxti" value="40" class="radioclass">40
  23.                                         <input type="radio" name="maxti" value="100" class="radioclass">100
  24.                                        
  25.                                         <!--空两行-->
  26.                                         <br /><br />

  27.                                         <!--算术类型-->
  28.                                         <button onclick="add()" class="bluebut">加法</button>
  29.                                         <button onclick="subtract()" class="bluebut">减法</button>
  30.                                         <button onclick="multiply()" class="bluebut">乘法</button>
  31.                                         <button onclick="division()" class="bluebut">除法</button>
  32.                                         <button onclick="randomarith()" class="bluebut">混合运算</button>
  33.                                         <button style="float:right" onclick="submit()" class="bluebut">提交</button>
  34.                                
  35.                                 </div>
  36.                                 <hr>
  37.                                 <!--题目-->
  38.                                 <div class="four-col">
  39.                                         <output id="questions"></output>
  40.                                 </div>
  41.                                 <hr>
  42.                                 <h5 class="watchout">注意:提交题目后,答对输入框为<font color="green">绿色</font>,答错则为<font color="red">红色</font>, 结果最多保留2位小数点(不四舍不入)!</h5>
  43.                                 <h5 class="auth">---by 千公子</h5>
  44.                         </div>
  45.                 </div>
  46.         </body>
  47. </html>
复制代码


page.css

                               
登录/注册后可看大图

  1. /* 主体 */
  2. body {
  3.         font-family: Verdana, Arial, Helvetica, sans-serif;
  4.         font-size: 15px;
  5. }

  6. h1 {
  7.         font-size: 60px;
  8.         text-shadow: 5px 5px 15px black, 0px 0px 2px black;
  9.         /* 标题颜色 */
  10.         color: linen;
  11. }

  12. /* 分割水平线 */
  13. hr {
  14.         height: 10px;
  15.         border: none;
  16.         background: linear-gradient(to right, green , blue); /* 标准的语法 */&#160;
  17. }

  18. /* 最下栏注意 */
  19. h5.watchout {
  20.         float: left;
  21.         background: yellow;
  22. }

  23. /* 作者信息 */
  24. h5.auth {
  25.         float: right;
  26. }

  27. /* 最外层div */
  28. #wrapper {
  29.         margin: 0 auto;
  30.         width: 85%;
  31. }

  32. #content {
  33.         float: left;
  34.         background: #fff;
  35.         width: 100%;
  36. }

  37. /* 题目排列 */
  38. .four-col {
  39.         -moz-column-count: 4;
  40.         -moz-column-gap: 20px;
  41.         -webkit-column-count: 4;
  42.         -webkit-column-gap: 20px;
  43.         column-count: 4;
  44.         column-gap: 20px;
  45. }

  46. /* 按钮样式 */
  47. .bluebut {
  48.         position: relative;
  49.         vertical-align: top;
  50.         width: 160px;
  51.         height: 50px;
  52.         padding: 0;
  53.         font-size: 22px;
  54.         color: white;
  55.         text-align: center;
  56.         text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  57.         background: #1abc9c;
  58.         border: 0;
  59.         border-bottom: 2px solid #12ab8d;
  60.         cursor: pointer;
  61.         -webkit-box-shadow: inset 0 -2px #12ab8d;
  62.         box-shadow: inset 0 -2px #12ab8d;
  63. }

  64. /* 提交答案输入框下划线 */
  65. output input{
  66.         border: 0;
  67.         border-bottom: 1px solid black;
  68. }
  69. /* 去除点击时框框样式 */
  70. input:focus{
  71.         outline: none;
  72. }

  73. /* 按钮点击 */
  74. .bluebut:active {
  75.         top: 1px;
  76.         outline: none;
  77.         -webkit-box-shadow: none;
  78.         box-shadow: none;
  79. }

  80. #head {
  81.         font-size: 22px;
  82. }

  83. input {
  84.         font-size: 22px;
  85.         width: 50px;
  86. }
复制代码


function.js

                               
登录/注册后可看大图

  1. // 初始化 min
  2. function initmin() {
  3.         // Number() 强转换, 相当于C语言的int() , (int)
  4.         min = Number(document.getElementById("min").value);
  5.         if (!Number.isInteger(min)) {
  6.                 alert("最小取值应该为整数!");
  7.         }
  8. }

  9. // 初始化 max
  10. function initmax() {
  11.         max = Number(document.getElementById("max").value);
  12.         if (!Number.isInteger(max)) {
  13.                 alert("最大取值应该为整数!");
  14.         }
  15. }

  16. //获取题目数量函数
  17. function initquesnum() {
  18.         var temp = document.getElementsByName("maxti");
  19.         for (i = 0; i < temp.length; i++) {
  20.                 //遍历Radio, 找出复选框按钮在哪,再取出里面的值
  21.                 if (temp[i].checked) {
  22.                         maxti = temp[i].value;
  23.                 }
  24.         }
  25. }

  26. // 初始化 max, min
  27. function init() {
  28.         initmin();
  29.         initmax();
  30.         initquesnum();
  31.         operators = ['+', '-', 'x', '÷'];

  32.         if (max < min) {
  33.                 alert("数值范围错误!")
  34.         }
  35. }


  36. // 识别几位数
  37. function figureformstr(str) {
  38.         return (str + '').length;
  39. }

  40. // 传入数值,返回一定数量的空格
  41. function addsp(num) {
  42.         // 最高位数
  43.         var fulldig = figureformstr(max);
  44.         // 传入数值位数
  45.         var numsp = figureformstr(num);
  46.         return new Array(fulldig - numsp + 1).join('&ensp;');
  47. }

  48. // 生成式子
  49. // i 第i个问题
  50. // f 第一个数字 if-->id
  51. // o 运算操作符        io-->id
  52. // s 第二个数字 is --> id
  53. // 答案 ians-->name
  54. function generateForm(i, f, o, s) {
  55.         return addsp(f) + "<span id='" + i + "f'>" + f + '</span>' +
  56.                 ' ' + "<span id='" + i + "o'>" + o + '</span>' + ' ' + addsp(s) +
  57.                 "<span id='" + i + "s'>" + s + "</span>" + ' = ' +
  58.                 " <input type='text' id='" + i + "ans'" +
  59.                 " onChange='istyle(this.id)' /><br/>";
  60. }

  61. //加法
  62. function add() {
  63.         // i 用作循环, formula生成的算术题目
  64.         var i, formula = '';
  65.         // 初始化数值
  66.         init();

  67.         for (i = 0; i < maxti; ++i) {
  68.                 n1 = parseInt((max - min) * Math.random() + min);
  69.                 n2 = parseInt((max - min) * Math.random() + min);

  70.                 formula = formula + generateForm(i, n1, operators[0], n2);
  71.         }
  72.         document.getElementById("questions").innerHTML = formula;
  73. }

  74. //随机减法函数
  75. function subtract() {
  76.         // i 用作循环, formula生成的算术题目
  77.         var i, formula = '';
  78.         // 初始化数值
  79.         init();

  80.         for (i = 0; i < maxti; ++i) {
  81.                 n1 = parseInt((max - min) * Math.random() + min);
  82.                 n2 = parseInt((max - min) * Math.random() + min);
  83.                 // 保证被减数始终比减数大
  84.                 if (n1 < n2) {
  85.                         n1 ^= n2;
  86.                         n2 ^= n1;
  87.                         n1 ^= n2;
  88.                 }
  89.                 formula = formula + generateForm(i, n1, operators[1], n2);
  90.         }
  91.         document.getElementById("questions").innerHTML = formula;
  92. }

  93. // 随机乘法函数
  94. function multiply() {
  95.         // i 用作循环, formula生成的算术题目
  96.         var i, formula = '';
  97.         // 初始化数值
  98.         init();

  99.         for (i = 0; i < maxti; ++i) {
  100.                 n1 = parseInt((max - min) * Math.random() + min);
  101.                 n2 = parseInt((max - min) * Math.random() + min);

  102.                 formula = formula + generateForm(i, n1, operators[2], n2);
  103.         }
  104.         document.getElementById("questions").innerHTML = formula;
  105. }

  106. // 随机除法
  107. function division() {
  108.         // i 用作循环, formula生成的算术题目
  109.         var i, formula = '';
  110.         // 初始化数值
  111.         init();

  112.         for (i = 0; i < maxti; ++i) {
  113.                 n1 = parseInt((max - min) * Math.random() + min);
  114.                 // 被除数不能为零
  115.                 do{
  116.                         n2 = parseInt((max - min) * Math.random() + min);
  117.                 }while(n2==0);
  118.                 formula = formula + generateForm(i, n1, operators[3], n2);
  119.         }
  120.         document.getElementById("questions").innerHTML = formula;
  121. }

  122. // 随机四则运算
  123. function randomarith() {
  124.         // i 用作循环, formula生成的算术题目
  125.         var i, formula = '',
  126.                 op = '';
  127.         // 初始化数值
  128.         init();

  129.         for (i = 0; i < maxti; ++i) {
  130.                 n1 = parseInt((max - min) * Math.random() + min);
  131.                 n2 = parseInt((max - min) * Math.random() + min);
  132.                 op = operators[parseInt(Math.random() * 4)];
  133.                 formula = formula + generateForm(i, n1, op, n2);
  134.         }
  135.         document.getElementById("questions").innerHTML = formula;
  136. }

  137. // 运算函数 '+', '-', 'x', '÷'
  138. function operate(n1, o, n2){
  139.         var rst;
  140.         n1 = parseInt(n1);
  141.         n2 = parseInt(n2);
  142.         switch(o){
  143.                 case '+':
  144.                         rst = n1 + n2;
  145.                         break;
  146.                 case '-':
  147.                         rst = n1 - n2;
  148.                         break;
  149.                 case 'x':
  150.                         rst = n1 * n2;
  151.                         break;
  152.                 case '÷':
  153.                         rst = n1 / n2;
  154.                         rst = Math.round(rst*100)/100;
  155.                         break;
  156.         }
  157.         // 调试
  158.         // var dbg = n1 + ' ' + o + ' ' + n2 + ' = ' + rst;
  159.         // console.log(dbg);
  160.         return rst;
  161. }

  162. //提交
  163. function submit() {
  164.         if(document.getElementById('questions').innerHTML==''){
  165.                 alert("题目尚未生成!");
  166.         }
  167.         // ans 答题上来的答案       
  168.         // rst 正确答案
  169.         var i, n1, n2, o, ans, rst, rgt=0;
  170.         for(i=0; i<maxti; ++i){
  171.                 // document.getElementById获取的是html对象
  172.                 n1 = document.getElementById(i + 'f').innerText;
  173.                 o = document.getElementById(i + 'o').innerText;
  174.                 n2 = document.getElementById(i + 's').innerText;
  175.                 ans = document.getElementById(i + 'ans');
  176.                
  177.                 // 调试
  178.                 // var dbg = n1 + ' ' + o + ' ' + n2 + ' = ' + ans;
  179.                 // console.log(dbg);
  180.                
  181.                 rst = operate(n1, o, n2);
  182.                 if(ans.value!='' && ans.value==rst){
  183.                         rgt++;
  184.                         //答对下划线变绿
  185.                         ans.style['border-bottom'] = '1px solid green';
  186.                         ans.style['color'] = 'green';
  187.                 }else{
  188.                         // 答错下划线边红
  189.                         ans.style['border-bottom'] = '1px solid red';
  190.                         ans.style['color'] = 'red';
  191.                 }
  192.         }
  193.         alert("得分: " + rgt/maxti*100);
  194. }

  195. // 进入输入框是边回原来的颜色
  196. function istyle(id){
  197.         var input_obj = document.getElementById(id);
  198.         // 判断字符是否为空
  199.         var value = input_obj.value;
  200.         if(value=='' || value==null || typeof value == "undefined"){
  201.                 return;
  202.         }else if(isNaN(value)){
  203.                 // 如果输入内容非数字,则设为空,重新输入
  204.                 input_obj.value = '';
  205.         }else{
  206.                 // 如果原先打过分,后面修改了值,则变为原始的黑色
  207.                 input_obj.style['color'] = 'black';
  208.                 input_obj.style['border-bottom'] = '1px solid black';
  209.         }
  210. }
复制代码


哪儿有疑问,回复!看到就解答
TIM截图20200416111730.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-16 11:52:05 | 显示全部楼层
要了要了!谢谢大佬!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-16 12:29:06 | 显示全部楼层
这个是?
捕获.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-16 12:34:16 | 显示全部楼层

图片!删除了这个标签就好了
  1. <img src="by.png" style="float: right; width:29.5px; height: 41.3px ;">
复制代码
!或者自己加一个啥图片上去!图片名字by.png。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-16 12:34:41 | 显示全部楼层
千公子 发表于 2020-4-16 12:34
图片!删除了这个标签就好了!或者自己加一个啥图片上去!图片名字by.png。

好,谢谢解答
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-16 12:39:42 | 显示全部楼层
初学就这么吊的吗厉害了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-16 15:04:00 | 显示全部楼层
本帖最后由 千公子 于 2020-4-16 15:13 编辑
最后的魁拔 发表于 2020-4-16 12:39
初学就这么吊的吗厉害了


这个我记得是我们班第一还是第二节课js课的作业!这个确实是初学的,我觉得这个代码写的简单,很适合拿来学习js!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 05:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表