• 替代jquery中的几个函数


    // https://open.alipay.com/developmentAccess/developmentAccess.htm
    
        var $ = window.jQuery;
    
        (function() {
          if (!NodeList.prototype.forEach) {
            NodeList.prototype.forEach = function(action) {
              for(var i = 0, l = this.length; i < l; i++) {
                action(this[i]);
              }
            };
          }
          var q = document.querySelectorAll.bind(document);
          var on = function(dom, event, cb) {
            if (!dom) return;
            if ('length' in dom) {
              return dom.forEach(function(elem){
                on(elem, event, cb);
              });
            }
            dom.addEventListener(event, cb);
          };
          var hasClass = function(dom, className) {
            if (!dom) return;
            if ('length' in dom) {
              for(var i = 0, l = dom.length; i < l; i++){
                if (hasClass(dom[i], className)) return true;
              }
              return false;
            }
            if (dom.classList) return dom.classList.contains(className);
            return dom.className.split(' ').indexOf(className) >= 0;
          };
          var addClass = function(dom, className) {
            if (!dom) return;
            if ('length' in dom) {
              return dom.forEach(function(elem){
                addClass(elem, className);
              });
            }
            if (dom.classList) return dom.classList.add(className);
            dom.className += ' ' + className;
          };
          var removeClass = function(dom, className) {
            if (!dom) return;
            if ('length' in dom) {
              return dom.forEach(function(elem){
                removeClass(elem, className);
              });
            }
            if (dom.classList) return dom.classList.remove(className);
    
            var length = className.length;
            if (dom.className.indexOf(className + ' ') === 0) return dom.className = dom.className.substr(length);
            if (dom.className.indexOf(' ' + className) === dom.className.length - length - 1) return dom.className = dom.className.substr(0, dom.className.length - length - 1);
           
            console.log(dom.className);
            return dom.className = dom.className.replace(' ' + className + ' ', '');
          };
          var parents = function(dom, className) {
            if (!dom) return null;
            var p = dom.parentNode;
            while(p) {
              if (hasClass(p, className)){
                return p;
              }
              p = p.parentNode;
            }
            return p;
          };
          // 点击登录
          on(q('#open-menu-login'), 'click', function(e){
            var curUrl=document.location.href;
            var loginUrl = "https://auth.alipay.com/login/ant_sso_index.htm?goto=" + encodeURIComponent(curUrl);
            window.location.href=loginUrl;
            e.preventDefault();
          });
          //点击退出
          on(q('.logout-link'), 'click', function(e){
            var logoutUrl=encodeURIComponent(document.location.href);
            window.location.href="https://auth.alipay.com/login/logout.htm?goto=" + logoutUrl;
            e.preventDefault();
          });
          // 判断子菜单为当前页面,一级导航高亮显示
          q('.open-submenu-item').forEach(function(item){
            if(hasClass(item, 'selected')){
              var parent = parents(item, 'open-menu-item');
              parent && addClass(parent, 'selected');
              parent && addClass(parent, 'can-disabled-select');
            }
          });
          // 点击搜索出现搜索框
          on(q('#nav-search'), 'click', function(event){
            addClass(q('#open-nav-right-menu'), 'open-search-mode');
            q('#J_SearchKeyword')[0].focus();
          });
          //点击其他区域搜索框消失
          on(q('#nav-search-input'), 'click', function(event){
            event.stopPropagation();
          });
          on(document, 'click', function(){
            removeClass(q('#open-nav-right-menu'), 'open-search-mode');
          });
    
                window.ENV = (function() {
            var host = window.location.host;
            var env = 'prod';
    
            if (host.indexOf('.alipay.com') >= 0 && false) {
              env = 'pre';
            } else if (host.indexOf('test.alipay.net') >= 0) {
              env = 'test';
            } else if (host.indexOf('.alipay.net') >= 0) {
              env = 'dev';
            }
            return env;
          })();
        }());
    

      

  • 相关阅读:
    线性回归(Linear Regression)的理解及原理
    3个模型搞清楚用户留存分析
    机器学习简单介绍
    数据分析经典方法:5W2H分析法
    使用guava RateLimiter限流
    Maven之assembly自定义打包
    IDE自动编译
    神奇的$scope
    二分法查找
    深入理解CSS选择器优先级
  • 原文地址:https://www.cnblogs.com/mingzhanghui/p/9198905.html
Copyright © 2020-2023  润新知