//---------------上传图片按钮点击------------ $(".btn_upload_file").change(function(){ var picurl = getObjectURL(this.files[0]); if(picurl){ $(".div_show_pic").show().attr("src",picurl); } });
2、实现 sleep 函数
很多语言都有 sleep 函数,显然 js 没有,所以需要其他的方法“模拟”实现 sleep 函数
方法一:通过比较当前时间
1 2 3 4 5 6 7 8 9
functionsleep(numberMillis) { var now = newDate(); var exitTime = now.getTime() + numberMillis; while (true) { now = newDate(); if (now.getTime() > exitTime) return; } }
document.body.oncopy = function () { setTimeout(function (){ var text = clipboardData.getData("text"); if (text) { text = text + "\\r\\n本篇文章来源于 www.地址.COM 原文链接:"+location.href; clipboardData.setData("text", text); } }, 100) }
4、很简单的省略字数
1 2 3 4 5 6 7
$(".omit_word_class").each(function(){ var ntext = $(this).text(); var nlen = $(this).text().length; if(nlen> 7){ $(this).text(ntext.substring(0,7) + "..."); } });
$.fn.extend({ displayPart:function () { var displayLength = 100; displayLength = this.attr("displayLength") || displayLength; var text = this.text(); if (!text) return"";
var result = ""; var count = 0; for (var i = 0; i < displayLength; i++) { var _char = text.charAt(i); if (count >= displayLength) break; if (/[^x00-xff]/.test(_char)) count++; //双字节字符,//[u4e00-u9fa5]中文 result += _char; count++; } if (result.length < text.length) { result += "..."; } this.text(result); } });
(functiongetDateStr(AddDayCount){ var dd = newDate(); dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期 var y = dd.getFullYear(); var m = dd.getMonth()+1;//获取当前月份的日期 var d = dd.getDate(); return y+"-"+m+"-"+d; })(-2);
11、根据屏幕宽度改变 html 的 font-size 大小
结合 REM 可以很轻松的实现移动端的响应式大小效果
1 2 3 4 5 6 7 8 9 10 11
(function(win) { functionsetUnitA() { document.documentElement.style.fontSize = document.documentElement.clientWidth / 16 + "px"; } var h = null; window.addEventListener("resize", function() { clearTimeout(h); h = setTimeout(setUnitA, 300); }, false); setUnitA(); })(window);