原生js写一个简易键盘,不可拼音

<!DOCTYPE html>
<html>

<head>
    <title>JavaScript 键盘演示</title>
</head>
<style>
    body {
        font-family: Arial, sans-serif;
    }

    #keyboard {
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    .row {
        display: flex;
        flex-direction: row;
    }

    .key {
        margin: 5px;
        padding: 10px;
        border: none;
        background-color: #f2f2f2;
        color: #333;
        font-size: 16px;
        cursor: pointer;
    }

    .key:hover {
        background-color: blue;
    }

    .inp {
        display: flex;
        justify-content: center;
        margin-bottom: 20px;
    }

    #inputField {
        padding: 10px;
        border: 2px solid #ccc;
        border-radius: 5px;
        font-size: 16px;
        transition: border-color 0.3s ease;
    }

    #inputField:focus {
        border-color: #007bff;
        outline: none;
    }

    .custom-button {
            display: inline-block;
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            text-align: center;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .custom-button:hover {
            background-color: #45a049;
        }
</style>

<body>
    <div class="inp">
        <input type="text" id="inputField" placeholder="输入...">
        <!-- <button onclick="clearInput()">X</button> -->
        <button class="custom-button" onclick="clearInput()">clear</button>
    </div>
    <div id="keyboard"></div>


    <script>
        const keyboard = document.getElementById('keyboard');
        const inputField = document.getElementById('inputField');

        // 定义键盘布局
        const keyboardLayout = [
            ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
            ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
            ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
            ['z', 'x', 'c', 'v', 'b', 'n', 'm']
        ];

        // 创建键盘按钮
        keyboardLayout.forEach(row => {
            const rowDiv = document.createElement('div');
            rowDiv.classList.add('row');
            row.forEach(key => {
                const button = document.createElement('button');
                button.classList.add('key');
                button.innerHTML = key.toUpperCase();
                button.addEventListener('click', () => {
                    if (key === 'Enter') {
                        inputField.value += '\n';
                    } else {
                        inputField.value += key;
                    }
                });
                rowDiv.appendChild(button);
            });
            keyboard.appendChild(rowDiv);
        });

        function clearInput() {
            inputField.value = "";
        }
    </script>
</body>

</html>

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值