主頁 > 知識庫 > html5移動端價格輸入鍵盤的實現(xiàn)

html5移動端價格輸入鍵盤的實現(xiàn)

熱門標(biāo)簽:使用U盤裝系統(tǒng) 美圖手機 硅谷的囚徒呼叫中心 檢查注冊表項 阿里云 智能手機 百度競價點擊價格的計算公式 網(wǎng)站建設(shè)

簡單實現(xiàn)移動端輸入價格鍵盤

HTML:

<div class="main">
    <div id="show-price">

    </div>
    <div class="keyboard">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
        <div>9</div>
        <div>.</div>
        <div>0</div>
        <div>刪除</div>
    </div>
</div>

CSS:

.keyboard {
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 240px;
    display: flex;
    flex-wrap: wrap;
}

.keyboard div {
    width: 30%;
    height: 50px;
    margin: 5px;
    text-align: center;
    line-height: 50px;
    border-radius: 5px;
    background: #eee;
}

JS:

<script src="../js/jquery-3.4.1.min.js"></script>
<script>
    window.onload = function () {
        let key = $('.keyboard div');
        let keyStr = ''
        key.click(function () {
            let str = ''
            let eleStr = $(this).html() == '刪除' ? '' : $(this).html(); // 是否刪除
            if(keyStr.length <= 0 && eleStr == '0') return; // 首位不能為0
            keyStr = keyStr + eleStr; // 拼接點擊的數(shù)字
            if(eleStr == '') keyStr = keyStr.substr(0, keyStr.length - 1); // 刪除
            for (let i = 0; i < keyStr.length; i++) { // 遍歷
                if (keyStr[i] == '.') { // 判斷是否為.
                    if (str.indexOf('.') == -1) str = str + keyStr[i]; // 是.并且其中不存在
                } else str = str + keyStr[i]; // 不是.就拼接
            }
            showPrice($('#show-price'), str); // 渲染
        })

        function showPrice(ele, str) {
            let htmlStr = ''
            for (let i = 0; i < str.length; i++) {
                htmlStr = htmlStr + `<span>${str[i]}</span>`
            }
            ele.html(htmlStr);
        }
    </script>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:懷化 煙臺 湘潭 黃山 湖北 通遼 賀州 山南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html5移動端價格輸入鍵盤的實現(xiàn)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266