几个简单实用的javascript代码块分享
1.获取地址栏参数
function geturlpara(paraname){
var surl = location.href;
var sreg = "(?:\\?|&){1}"+paraname+"=([^&]*)";
var re=new regexp(sreg,"gi");
re.exec(surl);
return regexp.$1;
}
//应用实例:test_para.html?a=11&b=22&c=33
console.log(geturlpara("a")); 、
console.log(geturlpara("b"));
2.发送验证码计时器
<input id="send" type="button" value="发送验证码">
var times = 60, // 临时设为60秒
document.getelementbyid('send').onclick = function () {
timer = setinterval(function () {
times--;
if (times <= 0) {
send.value = '发送验证码';
clearinterval(timer);
send.disabled = false;
times = 60; }
else {
send.value = times + '秒后重试';
send.disabled = true;
}
}, 1000);
}
3.数值每三位加逗号(钱转换)
function tothousands(number) {
var arr = string(number).split('.');
var num = arr[0], result = '';
while (num.length > 3) {
result = ',' + num.slice(-3) + result;
num = num.slice(0, num.length -3);
}
if (num.length > 0) result = num + result;
return arr[1] ? result + '.' + arr[1] : result
}
}
4.快速求数组的平均数(reduce,箭头函数)
const average = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
// average([1,2,3]) -> 2
5.数组去重(es6)
const unique = arr => [...new set(arr)];
// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
6.数组对象方法排序(按照对象的某个属性)
function sortbykey(array,key){
return array.sort(function(a,b){
var x=a[key];
var y=b[key];
return ((x<y)?-1:((x>y)?1:0));
})
}
7.输入框限定字符的个数(一个汉字算两个字符)
function getbyteval(val, max) {
var returnvalue = '';
var bytevallen = 0;
for (var i = 0; i < val.length; i++) {
if (val[i].match(/[^\x00-\xff]/ig) != null)
bytevallen += 2;
else
bytevallen += 1;
if (bytevallen > max)
break;
returnvalue += val[i];
}
return returnvalue;
}
$('#txt').bind('keyup',function(){
var val=this.value;
if(val.replace(/[^\x00-\xff]/g,"**").length>14){
this.value=getbyteval(val,14)
}
})
8.将时间戳转成yyyy-mm-dd hh:mm:ss的格式
/**
* timestamp:1469085472490
*/
var timestamptoformatdate = function(timestamp){
var date = new date(); date.settime(timestamp);
var year = date.getfullyear();
var month = date.getmonth() + 1;
var day = date.getdate();
var hour = date.gethours();
var minute = date.getminutes();
var second = date.getseconds();
return year + '-' + supplementzero(month,2) + '-' + supplementzero(day,2) + ' ' +
supplementzero(hour,2) + ':' + supplementzero(minute,2) + ':' + supplementzero(second,2);
}
9.返回顶部
function backtop(btnid) {
var btn = document.getelementbyid(btnid);
var d = document.documentelement;
var b = document.body;
window.onscroll = set;
btn.style.display = "none";
btn.onclick = function() {
btn.style.display = "none";
window.onscroll = null;
this.timer = setinterval(function() {
d.scrolltop -= math.ceil((d.scrolltop + b.scrolltop) * 0.1);
b.scrolltop -= math.ceil((d.scrolltop + b.scrolltop) * 0.1);
if ((d.scrolltop + b.scrolltop) == 0) clearinterval(btn.timer, window.onscroll = set);
},
10);
};
function set() {
btn.style.display = (d.scrolltop + b.scrolltop > 100) ? 'block': "none"
}
};
backtop('gotop');
9.jquery中$.post和$.ajax的用法
$.ajax({
type:'get',
url:'https://www.www.phpernote.com/rss',
beforesend:function(xmlhttprequest){
//showloading();
},
success:function(data,textstatus){
$('.ajax.ajaxresult').html('');
$('item',data).each(function(i,domele){
$('.ajax.ajaxresult').append('<li>'+$(domele).children('title').text()+'</li>');
});
},
complete:function(xmlhttprequest,textstatus){
//hideloading();
},
error:function(){
//请求出错处理
}
});
10.拖拽事件的封装
function drag(index){
index.onmousedown = function(event){
var ev = event ||window.event;
ev.preventdefault();
disx = ev.clientx-this.offsetleft;
disy = ev.clienty-this.offsettop;
//给index绑定moousemove事件
document.onmousemove = function(event){
var ev = event ||window.event;
ev.preventdefault();
var x = ev.clientx;
var y = ev.clienty;
index.style.left = x-disx+'px';
index.style.top = y-disy+'px';
}
}
document.onmouseup =function(){
document.onmousemove = null;
}
}
11.数组的搜索功能
var fruits = ['apple','banana','grapes','mango','orange'];、
function filteritems(query) {
return fruits.filter(function(el) {
return el.tolowercase().indexof(query.tolowercase()) > -1;
})
}
console.log(filteritems('ap'));
// ['apple', 'grapes']
11.求数组的最大值
//最大值
array.prototype.max = function() {
var max = this[0];
var len = this.length;
for (var i = 1; i < len; i++){
if (this[i] > max) {
max = this[i];
}
}
return max;
}
12.删除数组某一个元素
array.prototype.remove = function(val){
var index = this.indexof(val);
if(index !=0){
this.splice(index,1)
}
}
[1,3,4].remove(3)
下一篇: 什么时候是吃樱桃的季节
推荐阅读
-
几个简单实用的javascript代码块分享
-
企业网站内部链接优化的几个简单实用技巧分享
-
JavaScript登录记住密码操作的简单代码分享
-
php日历代码分享 简单实用的php日历代码
-
分享Javascript如何实现找不同色块游戏的示例代码分享
-
简单实用的js调试logger组件实现代码_javascript技巧
-
js设置文本框中焦点位置在最后的示例代码(简单实用)_javascript技巧
-
js设置文本框中焦点位置在最后的示例代码(简单实用)_javascript技巧
-
简单实用的js调试logger组件实现代码_javascript技巧
-
分享Javascript如何实现找不同色块游戏的示例代码分享