js检测开头输入空格

const str = JSON.stringify(arr);//将 JavaScript 值转换为 JSON 字符串,可以转函数或数组

 $(function () {
        $('input').bind('input',function () {
            let $val = $(this).val();
            let arr = [];
            for(let i =0;i<$val.length;i++){
                arr.push($val[i]);
            }
            console.log(arr);
            const str = JSON.stringify(arr);//将 JavaScript 值转换为 JSON 字符串,可以转函数或数组
            let reg = str.match(/\s+/g);
            //console.log(reg);
            // console.log(reg.length)
            $('span:first').html($val.length);
            $('span:last').html(reg.length)
        })
    })

在这里插入图片描述

发布了21 篇原创文章 · 获赞 5 · 访问量 822

猜你喜欢

转载自blog.csdn.net/printf_hello/article/details/104091226