16、获取任一元素的任意属性
function getStyle(elem, prop) {return window.getComputedStyle ? window.getComputedStyle(elem, null)[prop] : elem.currentStyle[prop]}17、绑定事件的兼容代码
function addEvent(elem, type, handle) {if (elem.addEventListener) { //非ie和非ie9elem.addEventListener(type, handle, false);} else if (elem.attachEvent) { //ie6到ie8elem.attachEvent('on' + type, function () {handle.call(elem);})} else {elem['on' + type] = handle;}}18、解绑事件
function removeEvent(elem, type, handle) {if (elem.removeEventListener) { //非ie和非ie9elem.removeEventListener(type, handle, false);} else if (elem.detachEvent) { //ie6到ie8elem.detachEvent('on' + type, handle);} else {elem['on' + type] = null;}}19、取消冒泡的兼容代码
function stopBubble(e) {if (e && e.stopPropagation) {e.stopPropagation();} else {window.event.cancelBubble = true;}}20、检验字符串是否是回文
function isPalina(str) {if (Object.prototype.toString.call(str) !== '[object String]') {return false;}var len = str.length;for (var i = 0; i < len / 2; i++) {if (str[i] != str[len - 1 - i]) {return false;}}return true;}21、检验字符串是否是回文
function isPalindrome(str) {str = str.replace(/W/g, '').toLowerCase();console.log(str)return (str == str.split('').reverse().join(''))}22、兼容getElementsByClassName方法
Element.prototype.getElementsByClassName = Document.prototype.getElementsByClassName = function (_className) {var allDomArray = document.getElementsByTagName('*');var lastDomArray = [];function trimSpace(strClass) {var reg = /s+/g;return strClass.replace(reg, ' ').trim()}for (var i = 0; i < allDomArray.length; i++) {var classArray = trimSpace(allDomArray[i].className).split(' ');for (var j = 0; j < classArray.length; j++) {if (classArray[j] == _className) {lastDomArray.push(allDomArray[i]);break;}}}return lastDomArray;}23、运动函数
function animate(obj, json, callback) {clearInterval(obj.timer);var speed,current;obj.timer = setInterval(function () {var lock = true;for (var prop in json) {if (prop == 'opacity') {current = parseFloat(window.getComputedStyle(obj, null)[prop]) * 100;} else {current = parseInt(window.getComputedStyle(obj, null)[prop]);}speed = (json[prop] - current) / 7;speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);if (prop == 'opacity') {obj.style[prop] = (current + speed) / 100;} else {obj.style[prop] = current + speed + 'px';}if (current != json[prop]) {lock = false;}}if (lock) {clearInterval(obj.timer);typeof callback == 'function' ? callback() : '';}}, 30)}24、弹性运动
function ElasticMovement(obj, target) {clearInterval(obj.timer);var iSpeed = 40,a, u = 0.8;obj.timer = setInterval(function () {a = (target - obj.offsetLeft) / 8;iSpeed = iSpeed + a;iSpeed = iSpeed * u;if (Math.abs(iSpeed) <= 1 && Math.abs(a) <= 1) {console.log('over')clearInterval(obj.timer);obj.style.left = target + 'px';} else {obj.style.left = obj.offsetLeft + iSpeed + 'px';}}, 30);}25、封装自己的forEach方法
Array.prototype.myForEach = function (func, obj) {var len = this.length;var _this = arguments[1] ? arguments[1] : window;// var _this=arguments[1]||window;for (var i = 0; i < len; i++) {func.call(_this, this[i], i, this)}}26、封装自己的filter方法
Array.prototype.myFilter = function (func, obj) {var len = this.length;var arr = [];var _this = arguments[1] || window;for (var i = 0; i < len; i++) {func.call(_this, this[i], i, this) && arr.push(this[i]);}return arr;}27、数组map方法
Array.prototype.myMap = function (func) {var arr = [];var len = this.length;var _this = arguments[1] || window;for (var i = 0; i < len; i++) {arr.push(func.call(_this, this[i], i, this));}return arr;}28、数组every方法
Array.prototype.myEvery = function (func) {var flag = true;var len = this.length;var _this = arguments[1] || window;for (var i = 0; i < len; i++) {if (func.apply(_this, [this[i], i, this]) == false) {flag = false;break;}}return flag;}29、数组reduce方法
Array.prototype.myReduce = function (func, initialValue) {var len = this.length,nextValue,i;if (!initialValue) {// 没有传第二个参数nextValue = https://www.isolves.com/it/cxkf/yy/js/2021-04-02/this[0];i = 1;} else {// 传了第二个参数nextValue = initialValue;i = 0;}for (; i < len; i++) {nextValue = func(nextValue, this[i], i, this);}return nextValue;}30、获取url中的参数
function getWindonHref() {var sHref = https://www.isolves.com/it/cxkf/yy/js/2021-04-02/window.location.href;var args = sHref.split('?');if (args[0] === sHref) {return '';}var hrefarr = args[1].split('#')[0].split('&');var obj = {};for (var i = 0; i
推荐阅读
- 鹿鞭人参黑松露的功效是什么?
- 什么材质的充气娃娃用的效果最好 买充气娃娃当女儿
- 金丝皇菊颜色非常黄正常吗,金丝皇菊挑选
- 如何在主流浏览器上正常使用只兼容IE的“上古网站”?
- 八种实用的免费游戏开发软件工具
- 怎样调节情绪
- 怎样去除腋臭
- 怎样去除湿热
- 浅表性胃炎的六大症状,胃炎的常见症状
- 白芷茶能经常喝吗,经常喝茶水能减肥吗
