js 1 2题怎么做 20

 我来答
百度网友a44bb7e
2019-03-01 · TA获得超过190个赞
知道小有建树答主
回答量:269
采纳率:69%
帮助的人:75.9万
展开全部

1.html代码

<!DOCTYPE html>
<!-- saved from url=(0032)http://www.wydclub.com/index.php -->
<html lang="zh-CN" style="transform: none;">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta name="renderer" content="webkit">
    <meta name="applicable-device" content="pc,mobile">
    <meta name="HandheldFriendly" content="true">
    <meta name="robots" content="index,follow">
    <title>HomeWork</title>
    <meta name="Keywords" content="HomeWork">
    <meta name="description" content="HomeWork">
    <script src="./index.js"></script>
    <link rel="stylesheet" rev="stylesheet" href="style.scss" type="text/css" media="all">
</head>
<body style="transform: none;">
    <div class="content">
        <div class="content_1">
 用户名:<input placeholder="请输入用户名(字母数字下划线组合且不能以下划线开头)" id="username" value="" onblur="checkCheck()">
            <span id="success">用户名可用</span>
            <span id="failed">用户名无效</span>
        </div>
        <div class="content_2">
            <div class="input-wrapper">
 请输入第一个数:<input type="number" onkeypress='return( /[\d]/.test(String.fromCharCode(event.keyCode) ) )'  placeholder="第一个数" value="" id="first">
            </div>
            <div class="input-wrapper">
 请输入第二个数:<input type="number" onkeypress='return( /[\d]/.test(String.fromCharCode(event.keyCode) ) )'  placeholder="第二个数" value="" id="second">
            </div>
            <div class="calc-btn">
                <button onclick="calcFun('+')">+</button>
                <button onclick="calcFun('-')">-</button>
                <button onclick="calcFun('*')">*</button>
                <button onclick="calcFun('/')">/</button>
            </div>
            <div class="input-wrapper">
 结果:<input id="result" value="" placeholder="计算结果">
            </div>
        </div>
    </div>
</body>
</html>

2.js代码

function  checkCheck(u) {
    u=document.getElementById('username').value;
    let f1 = false; //长度判断
    let f2 = false; //内容判断
    var re = /[0-9a-zA-Z_]/gi;
    if(u.length>=6 &&u.length <=16){
        f1 = true;
        if(re.test(u) && u.indexOf(0) !== '_'){
            f2 = true;
        }else {
            f2 = false;
        }
    }else {
        f1 = false;
    }
    if(f1 && f2){
        document.getElementById('failed').style.display = "none";
        document.getElementById('success').style.display = "inline-block";
        return true;
    }else {
        document.getElementById('success').style.display = "none";
        document.getElementById('failed').style.display = "inline-block";
        return false;
    }

}


function calcFun(arg) {
    let first = parseInt(document.getElementById('first').value);
    let second = parseInt(document.getElementById('second').value);
    let result = '';
    switch (arg) {
        case '+':
            result = first + second;
            break;
        case '-':
            result = first - second;
            break;
        case '*':
            result = first * second;
            break;
        case '/':
            result = first / second;
            break;
    }
    document.getElementById('result').setAttribute('value',result);

}

3.css代码 

*{
    margin : 0;
    padding : 0;
}

input::-webkit-input-placeholder { /* WebKit, Blink, Edge */
  color:#999;
  font-size: 12px;
  text-align: center;
}
input:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
  color:#999;
  font-size:  12px;
  text-align: -moz-center;
}
input::-moz-placeholder { /* Mozilla Firefox 19+ */
  color:#999;
  font-size:  12px;
  text-align: -moz-center;
}
input:-ms-input-placeholder { /* Internet Explorer 10-11 */
  color:#999;
  font-size:  12px;
  text-align: center;
}

input {
  width: 400px;
}
.content {
  box-sizing: border-box;
  margin-top: 100px;
}

.content_1, .content_2 ,.input-wrapper{
  margin: 20px auto;
  max-width: 1200px;
}

#success {
  display: none;
  color: green;
  padding-left: 15px;
}

#failed {
  display: none;
  color: red;
  padding-left: 15px;
}

.calc-btn button{
  width: 50px;
  padding: 5px 20px;
  display: table-cell;
  margin-left: 50px;
}

代码都在上面了,有什么不懂可以继续问,当然我只是简单实现了一下,不是什么标准答案,发现问题直接说哈;

最后,建议自己敲一遍代码,方便理解记忆

更多追问追答
追问
请问我这样写为什么会报错啊

介若灵78
2019-03-01
知道答主
回答量:86
采纳率:20%
帮助的人:9.4万
展开全部
首选获取input的文本值,然后绑定button的id,然后算法写上去就好了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友c697770
2019-03-01
知道答主
回答量:7
采纳率:0%
帮助的人:4654
展开全部
兄弟 这个 ....思路很简单 写起来不容易啊
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式