2017ife_yaoyao学院_task1

2017ife_yaoyao学院_task1

任务:

表单(一)单个表单项的检验

  • 任务目的
    加强对JavaScript的掌握
    熟悉常用表单处理逻辑
    任务描述
    如示例图中所示,在页面中实现一个输入框与按钮,要求点击验证按钮后,对输入框中内容进行格式校验,并在其下方显示校验结果
  • 校验规则
    1.字符数为4~16位
    2.每个英文字母、数字、英文符号长度为1
    3.每个汉字,中文符号长度为2
  • 任务注意事项
    要求功能实现与任务描述中完全一致
    示例图仅为参考,样式不需要完全实现一致
    请注意代码风格的整齐、优雅
    代码中含有必要的注释
    不允许借助任何第三方组件库实现

相关知识点:

  • 控制input type=”text”的高度和宽度

    input
    {
        height:30px;/*更改input的高度*/
    }
    
    <input type="text" name="username" id="username" size=40px/>
    
  • 提示的切换
    更换提示的时候应该将tr整行隐藏起来,id设在tr上,且隐藏和显示应采用以下语句
    document.getElementById(“error2”).style.display=”table-row”;
    document.getElementById(“init”).style.display=”none”;

  • 判断字符是中文还是英文
    中文:text.charCodeAt(i)>256
    英文:text.charCodeAt(i)<=256
  • github演示demo
    在github项目中html代码所在的网址前加htmlpreview.github.com/?就可以访问了[应该还会有更加好用的方法,这里的方法仅供参考]

代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>yaoyao-task1</title>
    <style>
        form{
            margin-left:50px;
            margin-top:50px;
        }
        label{
            font-size:18px;
            font-weight:bold;
        }
        input{
            height:30px;/*更改input的高度*/
            border-radius:8px;
            border:1px #999 solid;
        }
        #button{
            background-color:blue;
            color:white;
            border:1px blue solid;
            border-radius:4px;
            height:34px;
            font-size:18px;
            width:80px;
        }
        td{
            padding-top:10px;
        }
        /*提示部分*/
        #init{
            font-size:14px;
            color:#999;
        }
        #error1{
            font-size:14px;
            color:red;
            display:none;
        }
        #error2{
            font-size:14px;
            color:red;
            display:none;
        }
        #correct{
            font-size:14px;
            color:green;
            display:none;
        }
    </style>
    <script>
        function check(){
            var text=document.getElementById("username").value;
            var sum=0;
            if(text=="")
            {
                document.getElementById("error2").style.display="table-row";
                document.getElementById("init").style.display="none";
                document.getElementById("error1").style.display="none";
                document.getElementById("correct").style.display="none";
            }
            else
            {
                for(var i=0; i<text.length;i++)
                {
                    if(text.charCodeAt(i)>256)//是中文
                    {
                        sum=sum+2;
                    }
                    else
                    {
                        sum=sum+1;
                    }
                }
                console.log(sum);
                if(sum<4||sum>16)
                {
                    document.getElementById("error1").style.display="table-row";
                    document.getElementById("init").style.display="none";
                    document.getElementById("error2").style.display="none";
                    document.getElementById("correct").style.display="none";
                }
                else
                {
                    document.getElementById("correct").style.display="table-row";
                    document.getElementById("init").style.display="none";
                    document.getElementById("error2").style.display="none";
                    document.getElementById("error1").style.display="none";
                }
            }
        }
    </script>
</head>
<body>
    <form>
        <table>
        <tr>
          <td>
            <label>名称&nbsp;&nbsp;</label>
          </td>
          <td>
            <input type="text" name="username" id="username" size=40px/>
          </td>
          <td>
            <input type="button" id="button" onclick="check()" value="验证"/>
          </td>
        </tr>
        <tr id="init">
          <td></td>
          <td>
            <span>必填,长度为4~16个字符</span>
          </td>
        </tr>
        <tr id="error1">
          <td></td>
          <td>
            <span>输入字符长度不正确</span>
          </td>
        </tr>
        <tr id="error2">
          <td></td>
          <td>
            <span>姓名不能为空</span>
          </td>
        </tr>
        <tr id="correct">
          <td></td>
          <td>
            <span>名称格式正确</span>
          </td>
        </tr>
        </table>
    </form>
</body>
</html>
<!--
注意:更换提示的时候应该将tr整行隐藏起来,id设在tr上,且隐藏和显示应采用以下语句
                document.getElementById("error2").style.display="table-row";
                document.getElementById("init").style.display="none";
-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
折叠面板是一个常见的 UI 组件,可以在 Web 页面上实现内容的展开与收缩。以下是一个 HTML 右边折叠面板的实现示例: HTML 代码: ```html <div class="panel-container"> <div class="panel-header"> <h3>折叠面板标题</h3> <span class="panel-arrow"></span> </div> <div class="panel-content"> <p>折叠面板内容</p> </div> </div> ``` CSS 代码: ```css .panel-container { position: relative; width: 400px; height: 100%; } .panel-header { position: relative; z-index: 1; height: 40px; background-color: #f0f0f0; border-bottom: 1px solid #d9d9d9; cursor: pointer; } .panel-header h3 { margin: 0; padding: 0 20px; line-height: 40px; font-size: 16px; font-weight: bold; color: #333; } .panel-arrow { position: absolute; top: 50%; right: 20px; width: 0; height: 0; margin-top: -5px; border-left: 5px solid transparent; border-right: 5px solid transparent; border-top: 5px solid #999; transition: transform 0.3s ease; } .panel-content { position: absolute; top: 40px; right: 0; z-index: 0; width: 0; height: 0; overflow: hidden; background-color: #fff; transition: width 0.3s ease; } .panel-container.active .panel-header .panel-arrow { transform: rotate(180deg); } .panel-container.active .panel-content { width: 300px; height: 300px; } ``` 解释: 1. `.panel-container` 设置容器的宽度和高度,并设置为相对定位,以便内部元素进行定位。 2. `.panel-header` 设置折叠面板的标题栏样式,包括高度、背景色、边框和光标样式。 3. `.panel-header h3` 设置标题栏标题的样式,包括字体大小、颜色和粗细等。 4. `.panel-arrow` 设置标题栏右侧的箭头样式,包括定位、大小和颜色等。使用 `transition` 属性实现箭头旋转效果。 5. `.panel-content` 设置折叠面板的内容区样式,包括定位、大小、背景色和过渡效果等。使用 `overflow: hidden` 属性隐藏内容超出部分。 6. `.panel-container.active .panel-header .panel-arrow` 和 `.panel-container.active .panel-content` 通过 `.active` 类实现面板的展开和收缩效果。 以上是一个简单的 HTML 右边折叠面板的实现示例。可以根据实际需求调整样式和布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值