JS方法合集
虞南北
种一棵树最好的时间是在十年之前,其次是现在,世间之事向来如此
展开
-
js替换链接
页面中存在id=jsContainer的DOM元素。该DOM元素内会给出一段随机文本,可能包含一些链接,比如https://www.baidu.com,或者www.baidu.com?from=onlineExam,如果出现链接文本,请给该链接文本加上链接标签,用户点击后能直接在新窗口中打开该链接。请完成 link 函数,完成该功能 1、container只有纯文本内容,不包含其他dom元素2、识别所有以http://、https://或者www.开始的链接 3、所有www.开头的链接,默认使用 .原创 2021-09-29 10:00:08 · 1403 阅读 · 1 评论 -
js计时器
找出数组 arr 中重复出现过的元素(不用考虑返回顺序)例[1, 2, 4, 4, 3, 3, 1, 5, 3]//输入[1, 3, 4]//输出 function duplicates(arr) { var temp = []; arr.forEach(function(elem){ if(arr.indexOf(elem) != arr.lastIndexOf(elem) && temp.indexOf(elem) == -1){原创 2021-09-28 16:25:38 · 277 阅读 · 0 评论 -
js统计次数
统计数组 arr 中值等于 item 的元素出现的次数例:[1, 2, 4, 4, 3, 4, 3], 4//输入:3//输出:function count(arr, item) { var num = 0; arr.forEach(function(a){ if(a == item){ num++; } }); return num;}function count(arr, item) {原创 2021-09-28 15:39:06 · 573 阅读 · 0 评论 -
js数组合并
例:[1, 2, 3, 4], ['a', 'b', 'c', 1]//输入[1, 2, 3, 4, 'a', 'b', 'c', 1]//输出//利用concatfunction concat(arr1, arr2) { return arr1.concat(arr2);}//利用slice+push.apply function concat(arr1, arr2) { var newArr=arr1.slice(0); [].push.apply(newArr原创 2021-09-28 15:33:38 · 99 阅读 · 0 评论 -
js查找数组元素位置
找出元素 item 在给定数组 arr 中的位置如果数组中存在 item,则返回元素在数组中的位置,否则返回 -1例[ 1, 2, 3, 4 ], 3 // 输入:2 // 输出:function indexOf(arr, item) { return (arr.indexOf(item));}function indexOf(arr, item) {while(arr.length > 0) {return arr.indexOf(item);}re原创 2021-09-28 15:26:41 · 7316 阅读 · 0 评论 -
js字符串字符统计
统计字符串中每个字符的出现频率,返回一个 Object,key 为统计字符,value 为出现频率不限制 key 的顺序输入的字符串参数不会为空忽略空白字符例'hello world'//输入{h: 1, e: 1, l: 3, o: 2, w: 1, r: 1, d: 1}//输出function count(str) { str = str.trim().split(' '); let tempA = '';原创 2021-09-28 15:20:36 · 459 阅读 · 0 评论 -
js将字符串转换为驼峰格式
css 中经常有类似 background-image 这种通过 - 连接的字符,通过 javascript 设置样式的时候需要将这种样式转换成 backgroundImage 驼峰格式,请完成此转换功能以 - 为分隔符,将第二个起的非空单词首字母转为大写-webkit-border-image 转换后的结果为 webkitBorderImagefunction cssStyle2DomStyle(sName) { const reg = /-(.)/g; return sN原创 2021-09-28 15:16:18 · 3771 阅读 · 0 评论 -
js时间格式化输出
例//输入:formatDate(new Date(1409894060000), 'yyyy-MM-dd HH:mm:ss 星期w')//输出:2014-09-05 13:14:20 星期五function formatDate(t,str){ var obj = { yyyy:t.getFullYear(), yy:(""+ t.getFullYear()).slice(-2), M:t.getMonth()+1, MM:("0"+ (t.getMo原创 2021-09-28 15:12:18 · 535 阅读 · 0 评论 -
js数组去重
数组去重示例[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN] // 输入[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a'] // 输出Array.prototype.uniq = function () { return Array.from(new Set(this));}Array.prototype.uniq = functio原创 2021-09-28 15:07:24 · 93 阅读 · 0 评论 -
js获取 url 参数
js获取 url 参数获取 url 中的参数指定参数名称,返回该参数的值 或者 空字符串不指定参数名称,返回全部的参数对象 或者 {}如果存在多个同名参数,则返回数组不支持URLSearchParams方法function getUrlParam(sUrl, sKey) { let sParam = sUrl.split("?")[1].split("#")[0]; //key=1&key=2&key=3&test=4 let原创 2021-09-28 14:59:52 · 598 阅读 · 0 评论 -
js修改 this 指向
封装函数 f,使 f 的 this 指向指定的对象call()、bind()、apply()的用法,改变this的指向,区别在于f.call(obj, arg1, arg2…),f.bind(obj, arg1, arg2,…)(),f.apply(obj, [arg1, arg2, .])applyfunction bindThis(f, oTarget) { return function() { return f.apply(oTarget, arguments) }原创 2021-09-28 14:43:25 · 97 阅读 · 0 评论