javascript一些不错的函数脚本代码
2008-09-10 23:54
344 查看
计算字符长度的js函数
function LEN(str){
var i,sum=0;
for(i=0;i<str.length;i++){
if((str.charCodeAt(i)>=0) && (str.charCodeAt(i)<=255))
sum=sum+1;
else
sum=sum+2;
}
return sum;
}
去字符中前后的空格的js函数
复制代码 代码如下:
function TRIM(value){return value.replace(/^\s*/,'').replace(/\s*$/,''); }
图片自适应大小函数代码
复制代码 代码如下:
function imgAutoFit(imgObj,maxWidth,maxHeight){
var heightWidth;
var widthHeight;
heightWidth = imgObj.offsetHeight/imgObj.offsetWidth;
widthHeight = imgObj.offsetWidth/imgObj.offsetHeight;
if(imgObj.offsetWidth>maxWidth){
imgObj.width = maxWidth;
imgObj.height = maxWidth*heightWidth;
}
if(imgObj.offsetHeight>maxHeight){
imgObj.height = maxHeight;
imgObj.width = maxHeight*widthHeight;
}
}
function checkTagIsHave(s,t){
var re=new RegExp("<\\/?"+t+"(?:(?:\\s|\\/)(?:\\n|.)*?)?>","ig");
var r=re.test(s);
re=null;
return r;
}
function imgUploadFit(){
var u = document.getElementsByName("imgUploadName");
if(0==u.length) return;
for(var i=0;i<u.length;i++){
//imgAutoFit(u[i],560,560);
imgAutoFit(u[i],320,320);
u[i].onclick = function(){window.open(this.src);}
u[i].style.cursor = "pointer";
u[i].title = "点击看原图";
}
}
function inputContent(theQ,oldContent,re,p){
var o,oA=["msgframeamend","pingJiaFrame","touSuFrame"];
for(o in oA){
if($(oA[o])){
theQ.editIframe(oA[o]);
if("msgframeamend"==oA[o]) setTimeout(function(){$(oA[o]).contentWindow.document.body.innerHTML = oldContent.replace(re,"")+p[1];},100);
return;
}
}
}
function checkCommon(n,v,t){
var s = t?"补充提问":"回复内容";
var o=document.getElementById(n).contentWindow;
if("msgframereply" == n){
if(v == ""){
alert("请填写"+s+" !");
o.focus();
return false;
}
}
if(""==v.replace(/<\/*?(?:img|p|pre|br)(?:(?:\s|\/)(?:\n|.)*?)?>|\s| /ig,"")&&checkTagIsHave(v,"img")){
alert("针对图片,请附上一些说明性的文字!");
o.focus();
return false;
}
if(10000<LEN(v)){
alert(s+"超过5000字数限制 !");
o.focus();
return false;
}
return true;
}
相关文章推荐
- 一些实用的 Javascript 代码
- JavaScript常用检测脚本实例代码
- 9个实用的Javascript代码高亮脚本
- 巧妙利用函数的惰性载入提高javascript 代码性能
- mongodb 在slave中统计数据,然后保存到master的表中的javascript脚本代码
- 一些有用的javascript代码(1)
- 一些有用的javascript小函数
- javascript计算两个日期差的函数代码
- 分享一段通过前端javascript脚本 启用/禁用超链接的代码
- 关于时间函数的一些有用的SQL脚本
- Javascript 拖拽的一些简单的应用(逐行分析代码,让你轻松了拖拽的原理)
- 1、使用javascript代码写出一个函数:实现传入两个整数后弹出较大的整数
- 让firefox支持IE的一些方法的javascript扩展函数代码
- web一些值得珍藏的脚本代码
- IE预览添加javaScript代码的本地Html文件后会弹出阻止脚本框问题
- 一些常用的JavaScript帮助函数
- JavaScript中字符串处理的一些函数
- 一些经常会用到的Javascript检测函数
- 【PHP代码审计】 那些年我们一起挖掘SQL注入 - 6.全局防护Bypass之一些函数的错误使用