当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript一些不错的函数脚本代码

Javascript
JavaScript 渐变效果页面图片控制
JavaScript blog式日历控件新算法
Javascript 读后台cookie代码
js 弹簧效果代码
JavaScript滑移效果代码
javascript字符串拆分成单个字符相加和不超过10,求最终值
Prototype中dom对象方法汇总
Jquery与Prototype混合用法对比
javascript下IE与FF兼容函数收集
突破winxp sp2/win2003 sp2超强弹窗代码
js点击出现场层层外点击层消失的代码
Javascript模拟scroll滚动效果脚本
javascript各种复制代码收集
javascript的trim,ltrim,rtrim自定义函数
仿3721首页模块拖曳移动效果js代码[可拖曳层移动层]
Javascript拖拽系列文章1之offsetParent属性
Discuz! 6.1_jQuery兼容问题
js日历控件(可精确到分钟)
javascript DOM实用学习资料
javascript实现的树型下拉框改进版

Javascript 中的 javascript一些不错的函数脚本代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 75 ::
收藏到网摘: n/a

收集一些不多见的好用的自定义函数代码 计算字符长度的js函数 去字符中前后的空格的js函数 图片自适应大小函数代码

计算字符长度的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;
}