替代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; })(); }());