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)
    });
  • 相关阅读:
    阅读INI档
    jQuery遍历table中间tr td并获得td价值
    PB控制性能TreeView
    [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法
    数据结构 线性表
    ORACLE11G RAC 施加以分离不同的实例.TAF
    一起学习android使用一个回调函数onCreateDialog实现负载对话(23)
    [cocos2d-x 3.0] 触摸显示器
    lua学习笔记10:lua简单的命令行
    Matlab图像处理系列4———傅立叶变换和反变换的图像
  • 原文地址:https://www.cnblogs.com/zhanghuiming/p/7090036.html
Copyright © 2011-2022 走看看