JavaScript极速入门-综合案例(3)

综合案例

猜数字

预期效果

代码实现

html 复制代码
    <button type="button" id="reset">重新开始一局游戏</button>
    <br>
    请输入要猜的数字:<input type="text" id="number">
    <button type="button" id="button">猜</button>
    <br>
    已经猜的次数:<span id="count">0</span>
    <br>
    结果:<span id="result"></span>
    <script>
        $(function(){
            //先随机生成一个1-100的数字
            var guessNumber = Math.floor(Math.random() * 100) + 1; //Math.random()会生成1-100的数字
            var count = 0;
            //click:点击
            //事件驱动:只有真正发生了点击事件时,才会执行该函数
            $("#button").click(function() {
                count++;
                $("#count").text(count);
                var userGuess = parseInt($("#number").val());
                if(userGuess == guessNumber) {
                    $("#result").text("猜对了");
                    $("#result").css("color", "green");
                } else if(userGuess < guessNumber) {
                    $("#result").text("猜小了");
                    $("#result").css("color", "red");
                } else {
                    $("#result").text("猜大了");
                    $("#result").css("color", "red");
                }
                $("#number").val("");
            });
            $("#reset").click(function(){
                guessNumber = Math.floor(Math.random() * 100) + 1;
                count = 0;
                $("#count").text(count);
                $("#result").text("");
                $("#number").val("");
            });
        });
    </script>

表白墙

预期效果

**需求:**按要求在文本框中输入内容,点击提交按钮,输入内容显示在页面下方.

代码实现

1.提前准备如下HTML和CSS代码

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .container {
            width: 400px;
            margin: 0 auto;
        }

        h1 {
            text-align: center;
            padding: 20px 0;
        }

        p {
            color: #666;
            text-align: center;
            font-size: 14px;
            padding: 10px 0;
        }

        .row {
            height: 40px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        span {
            width: 100px;
            line-height: 40px;
        }

        .edit {
            width: 200px;
            height: 30px;
        }

        .submit {
            width: 304px;
            height: 40px;
            color: white;
            background-color: orange;
            border: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>表⽩墙</h1>
        <p>输⼊后点击提交, 会将信息显⽰在表格中</p>
        <div class="row">
            <span>谁: </span>
            <input class="edit" type="text">
        </div>
        <div class="row">
            <span>对谁: </span>
            <input class="edit" type="text">
        </div>
        <div class="row">
            <span>说什么: </span>
            <input class="edit" type="text">
        </div>
        <div class="row">
            <input type="button" value="提交" class="submit">
        </div>
    </div>
</body>

</html>

2.实现提交

html 复制代码
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"
        integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
    <script>
        $(function () {
            // 给点击按钮注册点击事件
            $(".submit").click(function () {
                // 1. 获取到编辑框内容
                var from = $('.edit:eq(0)').val();
                var to = $('.edit:eq(1)').val();
                var message = $('.edit:eq(2)').val();
                console.log(from + "," + to + "," + message);
                if (from == '' || to == '' || message == '') {
                    return;
                }
                // 2. 构造 html 元素
                var html = '<div class="row">' + from + '对' + to + '说: ' + message + '<div class="row">';
                // 3. 把构造好的元素添加进去
                $('.container').append(html);
                // 4. 同时清理之前输⼊框的内容
                $(":text").val("");
            });
        });
    </script>

总结

1.HTML是一种超文本标记语言,主要用于页面内容的显示和排版.如果需要更漂亮的样式展示和页面效果,需要搭配CSS和JavaScript来使用

2.学习HTML主要是学习标签,HTML标签特别多,了解基本语法即可

3.CSS重点是学习CSS选择器的使用

4.JavaScript是一个脚本语言,与Java没有关系.JQuery是一个JavaScript框架,使用JQuery可以轻松地选择和操作HTML元素,提高我们的开发效率.

5.前端开发对于后端开发人员而言不是很重要,不必花费过多时间在这个上面.达到借助网络能阅读代码的水平即可

相关推荐
郑州光合科技余经理2 小时前
代驾系统架构拆解:订单链路、权限组织与私有化源码交付
开发语言·后端·算法·架构·系统架构·uni-app·php
To_OC4 小时前
LC 35 搜索插入位置:二分查找谁都会,边界条件谁写谁懵
javascript·算法·leetcode
码哥DFS5 小时前
二叉树的直径
开发语言·javascript·算法
paopaokaka_luck5 小时前
基于springboot3+vue3的企业考勤管理系统(部门树递归、Echarts图形化分析)
开发语言·spring boot·学习·echarts·mybatis·需求分析·代码规范
梦雨生生6 小时前
java开发工具(学习第一天)
java·开发语言·学习
光影少年7 小时前
RN的Fabric 渲染流程
运维·前端·javascript·react native·react.js·fabric
啊啊啊啊啊!!!!8 小时前
【c++】stack和queue的接口使用以及底层实现
开发语言·c++
用户938515635078 小时前
React Router 进阶:路由守卫、登录鉴权与状态传递
前端·javascript·全栈
kyriewen8 小时前
我重写了自己用了两年的防抖节流Hook——发现里面藏着3个隐藏bug
前端·javascript·面试
松仔log9 小时前
Java中级——组合和继承
android·java·开发语言