原生JavaScript技巧大收集(1~10)
1、原生JavaScript实现字符串长度截取
01 |
function cutstr(str, len) { |
02 |
var temp; |
03 |
var icount = 0; |
04 |
var patrn = /[^\x00-\xff]/; |
05 |
var strre = ""; |
06 |
for (var i = 0; i < str.length; i++) { |
07 |
if (icount < len - 1) { |
08 |
temp = str.substr(i, 1); |
09 |
if (patrn.exec(temp) == null) { |
10 |
icount = icount + 1 |
11 |
} else { |
12 |
icount = icount + 2 |
13 |
} |
14 |
strre += temp |
15 |
} else { |
16 |
break |
17 |
} |
18 |
} |
19 |
return strre + "..." |
20 |
} |
2、原生JavaScript获取域名主机
01 |
function getHost(url) { |
02 |
var host = "null"; |
03 |
if(typeof url == "undefined"|| null == url) { |
04 |
url = window.location.href; |
05 |
} |
06 |
var regex = /^\w+\:\/\/([^\/]*).*/; |
07 |
var match = url.match(regex); |
08 |
if(typeof match != "undefined" && null != match) { |
09 |
host = match[1]; |
10 |
} |
11 |
return host; |
12 |
} |
3、原生JavaScript清除空格
1 |
String.prototype.trim = function() { |
2 |
var reExtraSpace = /^\s*(.*?)\s+$/; |
3 |
return this.replace(reExtraSpace, "$1") |
4 |
} |
4、原生JavaScript替换全部
1 |
String.prototype.replaceAll = function(s1, s2) { |
2 |
return this.replace(new RegExp(s1, "gm"), s2) |
3 |
} |
5、原生JavaScript转义html标签
1 |
function HtmlEncode(text) { |
2 |
return text.replace(/&/g, '&').replace(/\"/g, '"').replace(/</g, '<').replace(/>/g, '>') |
3 |
} |
6、原生JavaScript还原html标签
1 |
function HtmlDecode(text) { |
2 |
return text.replace(/&/g, '&').replace(/"/g, '\"').replace(/</g, '<').replace(/>/g, '>') |
3 |
} |
7、原生JavaScript时间日期格式转换
01 |
Date.prototype.Format = function(formatStr) { |
02 |
var str = formatStr; |
03 |
var Week = ['日', '一', '二', '三', '四', '五', '六']; |
04 |
str = str.replace(/yyyy|YYYY/, this.getFullYear()); |
05 |
str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100)); |
06 |
str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1)); |
07 |
str = str.replace(/M/g, (this.getMonth() + 1)); |
08 |
str = str.replace(/w|W/g, Week[this.getDay()]); |
09 |
str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate()); |
10 |
str = str.replace(/d|D/g, this.getDate()); |
11 |
str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours()); |
12 |
str = str.replace(/h|H/g, this.getHours()); |
13 |
str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes()); |
14 |
str = str.replace(/m/g, this.getMinutes()); |
15 |
str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds()); |
16 |
str = str.replace(/s|S/g, this.getSeconds()); |
17 |
return str |
18 |
} |
8、原生JavaScript判断是否为数字类型
1 |
function isDigit(value) { |
2 |
var patrn = /^[0-9]*$/; |
3 |
if (patrn.exec(value) == null || value == "") { |
4 |
return false |
5 |
} else { |
6 |
return true |
7 |
} |
8 |
} |
9、原生JavaScript设置cookie值
1 |
function setCookie(name, value, Hours) { |
2 |
var d = new Date(); |
3 |
var offset = 8; |
4 |
var utc = d.getTime() + (d.getTimezoneOffset() * 60000); |
5 |
var nd = utc + (3600000 * offset); |
6 |
var exp = new Date(nd); |
7 |
exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000); |
8 |
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;" |
9 |
} |
10、原生JavaScript获取cookie值
1 |
function getCookie(name) { |
2 |
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); |
3 |
if (arr != null) return unescape(arr[2]); |
4 |
return null |
5 |
} |
原生JavaScript技巧大收集(1~10)的更多相关文章
- 原生JavaScript技巧大收集100个
原生JavaScript技巧大收集 1.原生JavaScript实现字符串长度截取function cutstr(str, len) { var temp; var icount = 0; var p ...
- 原生JavaScript技巧大收集
原生JavaScript技巧大收集 地址:http://itindex.net/detail/47244-javascript
- 原生JavaScript技巧大收集(11~20)-(终于又被我找到这篇文章了)
11.原生JavaScript加入收藏夹 function AddFavorite(sURL, sTitle) { try { window.external.addFavorite(sURL, sT ...
- 原生态纯JavaScript 100大技巧大收集---你值得拥有
1.原生JavaScript实现字符串长度截取 function cutstr(str, len) { var temp; var icount = 0; var patrn = /[^\x00-\x ...
- 10个原生JavaScript技巧
这些代码片段主要由网友们平时分享的作品代码里面和经常去逛网站然后查看源文件收集到的.把平时网站上常用的一些实用功能代码片段通通收集起来,方便网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作 ...
- 分享10个原生JavaScript技巧
首先在这里要非常感谢无私分享作品的网友们,这些代码片段主要由网友们平时分享的作品代码里面和经常去逛网站然后查看源文件收集到的.把平时网站上常用的一些实用功能代码片段通通收集起来,方便网友们学习使用,利 ...
- javascript技巧大全套
事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); event.srcE ...
- 原生JavaScript技巧
时常在技术论坛有看见一些比较好的示例,于是就出于一种收集并学习的态度,于是就保留下来啦~ 当然现在展示的也只是一部分,先放一部分出来尝尝鲜~~~
- JavaScript常用,继承,原生JavaScript实现classList
原文链接:http://caibaojian.com/8-javascript-attention.html 基于 Class 的组件最佳实践(Class Based Components) 基于 C ...
随机推荐
- 使用node-webkit(v0.35.5)和innosetup(3.6.1)打包将web程序打包为桌面客户端(安装包)
这边主要是有一个客户,需要在电视机上安装一个客户端,含有视频直播功能:刚开始我们采用的webapp打包成apk安装在电视机上,发现摄像头监控画面根本无法播放(apk在手机上可以正常播放视频):排除一些 ...
- Json To CSharp
This is a tools for generate json reader classes. In some case, when we get a json data, we hope to ...
- Unity3d-通过简单示例来理解Time.deltaTime
转载文章: Unity3d-通过简单示例来理解Time.deltaTime 2018年04月21日 18:04:14 Black_Window 阅读数:926 标签: UnityTime 更多 个人分 ...
- 【zigbee 】2.4G信号发放器 AT2401C PA功放
概述 AT2401C 是一款面向Zigbee,无线传感网络以及其他2.4GHz 频 段无线系统的全集成射频功能的射频前端单芯片.AT2401C 是采用 CMOS 工艺实现的单芯片器件,其内部集成了功率 ...
- Vue.js 相关知识(脚手架)
1. vue-cli 简介 Vue-cli 是 vue的设计者,为提升开发效率而提供的一个脚手架工具,可通过vue-cli快速构造项目结构 2. vue-cli 安装步骤 安装npm 或 cnpm n ...
- c# winform调用摄像头识别二维码
首先我们需要引用两个第三方组件:AForge和zxing. Aforge是摄像头操作组件,zxing是二维码识别组件.都是开源项目.避免重复造轮子. 其实一些操作代码我也是参照别人的,若侵犯您的版权, ...
- 11.11 Daily Scrum
Today's tasks Tomorrow's tasks 丁辛 餐厅列表事件处理 餐厅列表事件处理 李承晗 实现指定地点搜索 整合已经完成的部 ...
- 【Alpha】第四次Scrum meeting
今天任务一览: 姓名 今日完成任务 所耗时间 刘乾 配置好了所有物理实验的通配模板,为服务器配置了latex中文环境,设置了一些常用字体. Issue链接:https://github.com/bua ...
- vue 有关框架
iview-admin https://github.com/iview/iview-admin/ axios https://www.kancloud.cn/yunye/axios/234845
- Python开发【第七章】:面向对象进阶
1.静态方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类 ...