zoukankan      html  css  js  c++  java
  • Js中遇到的坑点汇总

    一、Android 手机下输入框获取焦点时, 输入法挡住输入框的 bug

    解决思路:

    1.去掉overflow属性 

    2. Android 手机下, input 或 textarea 元素聚焦时, 主动滚一把

     if (/Android/gi.test(navigator.userAgent)) {
                window.addEventListener('resize', function () {
                    if (document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'TEXTAREA') {
                        window.setTimeout(function () {
                            document.activeElement.scrollIntoViewIfNeeded();
                        }, 0);
                    }
                })
    }

    二、js获取ip地址

    /**
     * Get the user IP throught the webkitRTCPeerConnection
     * @param onNewIP {Function} listener function to expose the IP locally
     * @return undefined
     */
    function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
        //compatibility for firefox and chrome
        var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
        var pc = new myPeerConnection({
            iceServers: []
        }),
        noop = function() {},
        localIPs = {},
        ipRegex = /([0-9]{1,3}(.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
        key;
    
        function iterateIP(ip) {
            if (!localIPs[ip]) onNewIP(ip);
            localIPs[ip] = true;
        }
    
         //create a bogus data channel
        pc.createDataChannel("");
    
        // create offer and set local description
        pc.createOffer(function(sdp) {
            sdp.sdp.split('
    ').forEach(function(line) {
                if (line.indexOf('candidate') < 0) return;
                line.match(ipRegex).forEach(iterateIP);
            });
            
            pc.setLocalDescription(sdp, noop, noop);
        }, noop); 
    
        //listen for candidate events
        pc.onicecandidate = function(ice) {
            if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
            ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
        };
    }
    
    // Usage
    
    getUserIP(function(ip){
            console.log('ip address : '+ip)
    });
  • 相关阅读:
    vs中添加wsdl生成代理类工具
    vscode+prettier 设置保存自动格式化
    k8s 部署项目
    Jmate使用
    k8s部署项目
    docker 打包镜像 部署项目
    vs2012编译xp运行的mfc程序InitializeCriticalSectionEx解决方案
    thinkphp 入口文件 iis 500错误
    java初学之stream
    php preg_match正则长度限制
  • 原文地址:https://www.cnblogs.com/zhanghuiming/p/7090036.html
Copyright © 2011-2022 走看看