HTML Dom结合js实例练习题:模拟用户登录验证

HTML Dom结合js实例练习题:模拟用户登录验证

在登录界面可以实现用户名,密码,验证码输入,点击登录按钮可以判断用户输入是否正确从而判断是否登录成功

参考程序:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>login</title>
</head>
<body>
    <div style="text-align: center;">
        <h4 >登录</h4>
        <hr>
        <p>账号: <input type="text" id="account" placeholder="请输入账号..."> </p>
        <p>密码: <input type="text" id="password" placeholder="请输入密码..."> </p>
        <p>
            验证码: <input type="text" id="code" style="width: 2cm;margin-right: 0.5cm;"> <span id="code1">3452</span>
            <span>
                <button id="btn" onclick="code()">刷新验证码</button>
            </span>
        </p>
        <button id="login" onclick="login()">登录</button>
    </div>
</body>
<script>
    var codes="";
    var str="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    function code(){
        codes="";
        for(let i=0;i<4;i++){
            // 随机下标
            var index=parseInt((Math.random())*str.length);
            // 生成4位验证码
            codes+=str[index];
        };
        // 将验证码赋值
        document.getElementById("code1").innerText=codes;
    }
    // 登录
    function login(){
        //账号:admin,密码;admin
        // 获取用户输入的账号,密码,验证码
        var account=document.getElementById("account");
        var password=document.getElementById("password");
        var code=document.getElementById("code");
        // 判断正确与否
        if(account.value=="admin"&&password.value=="admin"&&code.value==codes){
            alert("登录成功!")
        }else{
            alert("输入有误,请检查!")
        }}
    
</script>
</html>
测试:

点击刷新验证码按钮,可以刷新验证的显示值,当账号,密码,验证码都输入正确,才会登录成功!

copy.png

当输入信息有误,会有错误提示

copy.png

总结:

上面的程序中用到的知识点:

  1. 随机数生成:

    生成0-1范围内的数(包含0,不包含1):Math.random()

    生成一定范围的随机数: Math.random()*(你要随机数的最大值)+随机数的起始值

    比如:要生成1-10以内的随机数,可以这样写:Math.random()*10+1

  2. 常用dom对象的方法

    方法描述
    getElementById()返回带有指定ID的元素
    getElementsByTagName()返回包含带有指定标签名称的所有元素的节点列表
    getElementsByClassName()返回包含带有指定类名的所有元素的节点列表
  3. innerHTML属性:往指定对象设置显示文本属性,可以显示样式

    如:document.getElementById(“code1”).innerText=codes;

    当上面的codes写成

    codes

    时,其样式也会渲染到界面

  4. innerText属性是当值都当成纯文本处理


更多详细的内容可以查阅相关的文档资料哦,本次分享到这里,下一篇见!!

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
{ type: "searchSelect", placeholder: "签约机构", valueName: 'signOrganId', optionName: "label", searchItemName: "label", optionId: "key", searchApi:commonService.orgPageList({}).then(res=>{ const {retData}=res retData.map(item=>{ return {key: item.id, label: item.organName, value: item.id,} }) }) }, 分析一下此段代码的报错 汉语解释 ,并修改searchApi中的代码,index.jsx:55 Uncaught TypeError: item.searchApi is not a function at searchQuery (index.jsx:55:1) at onFocus (index.jsx:129:1) at onContainerFocus (BaseSelect.js:326:1) at HTMLUnknownElement.callCallback (react-dom.development.js:188:1) at Object.invokeGuardedCallbackDev (react-dom.development.js:237:1) at invokeGuardedCallback (react-dom.development.js:292:1) at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:306:1) at executeDispatch (react-dom.development.js:389:1) at executeDispatchesInOrder (react-dom.development.js:414:1) at executeDispatchesAndRelease (react-dom.development.js:3278:1) at executeDispatchesAndReleaseTopLevel (react-dom.development.js:3287:1) at forEachAccumulated (react-dom.development.js:3259:1) at runEventsInBatch (react-dom.development.js:3304:1) at runExtractedPluginEventsInBatch (react-dom.development.js:3514:1) at handleTopLevel (react-dom.development.js:3558:1) at batchedEventUpdates$1 (react-dom.development.js:21871:1) at batchedEventUpdates (react-dom.development.js:795:1) at dispatchEventForLegacyPluginEventSystem (react-dom.development.js:3568:1) at attemptToDispatchEvent (react-dom.development.js:4267:1) at dispatchEvent (react-dom.development.js:4189:1) at unstable_runWithPriority (scheduler.development.js:653:1) at runWithPriority$1 (react-dom.development.js:11039:1) at discreteUpdates$1 (react-dom.development.js:21887:1) at discreteUpdates (react-dom.development.js:806:1) at dispatchDiscreteEvent (react-dom.development.js:4168:1)
最新发布
07-14

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员-小李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值