zoukankan      html  css  js  c++  java
  • JS一些记录

    限制输入框输入数字:oninput = "value=value.replace(/[^d]/g,'')"        /g全局匹配 ^非

           数字加小数点      value=value.replace(/[^d^.]/g,'')

    复制到剪切板 text为要复制的内容

    f

    function copyText(text){
    // 数字没有 .length 不能执行selectText 需要转化成字符串
    const textString = text.toString();
    let input = document.querySelector('#copy-input');
    if (!input) {
    input = document.createElement('input');
    input.id = "copy-input";
    input.readOnly = "readOnly"; // 防止ios聚焦触发键盘事件
    input.style.position = "absolute";
    input.style.left = "-1000px";
    input.style.zIndex = "-1000";
    document.body.appendChild(input)
    }

    input.value = textString;
    // ios必须先选中文字且不支持 input.select();
    selectText(input, 0, textString.length);
    console.log(document.execCommand('copy'), 'execCommand');
    if (document.execCommand('copy')) {
    document.execCommand('copy');

    alert('已复制到粘贴板');

    }
    input.blur();

    // input自带的select()方法在苹果端无法进行选择,所以需要自己去写一个类似的方法
    // 选择文本。createTextRange(setSelectionRange)是input方法
    function selectText(textbox, startIndex, stopIndex) {
    if (textbox.createTextRange) {//ie
    const range = textbox.createTextRange();
    range.collapse(true);
    range.moveStart('character', startIndex);//起始光标
    range.moveEnd('character', stopIndex - startIndex);//结束光标
    range.select();//不兼容苹果
    } else {//firefox/chrome
    textbox.setSelectionRange(startIndex, stopIndex);
    textbox.focus();
    }
    }
    };

    JSON.stringify();转化成字符串

    JSON.parse();转化成json

    Math.ceil() 返回大于等于数字参数的最小整数(取整函数),对数字进行上舍入(只要整数,抹去小数+1)

    Math.floor() 返回小于等于数字参数的最大整数,对数字进行下舍入(只要整数,抹去小数)

    Math.round() 返回数字最接近的整数,四舍五入

  • 相关阅读:
    太原市圆通快递网点
    快递单号查询小工具
    C#快递单号查询源码
    爱快递快递接口使用说明
    如何把网站及数据库部署到Windows Azure
    从window.console&&console.log(123)浅谈JS的且运算逻辑(&&)
    C# Enum 简易权限设计 使用FlagsAttribute属性
    Lambda 表达式(C# 编程指南)
    C# list使用方法
    SharePoint Server 2013介绍v2
  • 原文地址:https://www.cnblogs.com/long7long/p/11024358.html
Copyright © 2011-2022 走看看