Javascript小函数
最近在学javascript,没事就写几个小函数,还有网上找的,挺有用的!
//自动调整图像大小,将图片限制在一定范围,保持比例平衡!
//我的想法是,获的原始图象大小,然后按百分比缩小,直到小于规定的大小。j是每次减去的量。可以调按实际调整。
//调用方法:<img id="a1" name="a1" src="图片路径 onload="AdjustImage(500,350,tihs.id)" /> 将图片限制在500,350比例范围内,保持比例。注意,不要写width,与height属性。
//IE6.0 IE7.0 firefox 试过了,好用,多张图片只要保持Id唯一就可
function AdjustImage(w,h,id)
{
var width=w,height=h;
var el=document.getElementById(id);
var imgWidth=el.width,imgHeight=el.height;
var i=100,j=1,newWidth,newHeight;
if(imgWidth>=width||imgHeight>=height)
{
while(i>0)
{
i=i-j;
newWidth=imgWidth*(i/100);
newHeight=imgHeight*(i/100);
if(newWidth<width&&newHeight<height) break;
}
el.width=newWidth;
el.height=newHeight;
}
}
//是否含有空格
function IsWhitespace (str)
{
var whitespace = " "t"n"r";
var i;
for (i = 0; i < str.length; i++){
var c = str.charAt(i);
if (whitespace.indexOf(c) >= 0) {
return true;
}
}
return false;
}
//去空格
function AllTrim(s)
{
if(s.substr(0,1)==" ")
{
do
{
s=s.substr(1,s.length-1);
}
while(s.substr(0,1)==" ")
}
if(s.substring(s.length,s.length-1)==" ")
{
do
{
s=s.substring(0,s.length-1);
}while(s.substring(s.length,s.length-1)==" ")
}
return s;
}
//是否是数字字符
function chkNum(ctr)
{
var strTemp="0123456789";
var numStr=ctr;
var j=strTemp.indexOf(numStr);
if(j==-1)
return false;
else
return true;
}
//打开新窗口
function openwin(url,width_,height_,left_,top_)
{
window.open (url, "newwindow", "height="+height_+", width="+width_+",left="+left_+",top="+top_+",toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no")
}
//转换成单精度浮点数
function tofloat(value)
{
value = Math.round(parseFloat(value)*100)/100;
if(value.toString().indexOf(".")<0)
value = value.toString()+".00";
return value;
}
//我的想法是,获的原始图象大小,然后按百分比缩小,直到小于规定的大小。j是每次减去的量。可以调按实际调整。
//调用方法:<img id="a1" name="a1" src="图片路径 onload="AdjustImage(500,350,tihs.id)" /> 将图片限制在500,350比例范围内,保持比例。注意,不要写width,与height属性。
//IE6.0 IE7.0 firefox 试过了,好用,多张图片只要保持Id唯一就可
function AdjustImage(w,h,id)
{
var width=w,height=h;
var el=document.getElementById(id);
var imgWidth=el.width,imgHeight=el.height;
var i=100,j=1,newWidth,newHeight;
if(imgWidth>=width||imgHeight>=height)
{
while(i>0)
{
i=i-j;
newWidth=imgWidth*(i/100);
newHeight=imgHeight*(i/100);
if(newWidth<width&&newHeight<height) break;
}
el.width=newWidth;
el.height=newHeight;
}
}
//是否含有空格
function IsWhitespace (str)
{
var whitespace = " "t"n"r";
var i;
for (i = 0; i < str.length; i++){
var c = str.charAt(i);
if (whitespace.indexOf(c) >= 0) {
return true;
}
}
return false;
}
//去空格
function AllTrim(s)
{
if(s.substr(0,1)==" ")
{
do
{
s=s.substr(1,s.length-1);
}
while(s.substr(0,1)==" ")
}
if(s.substring(s.length,s.length-1)==" ")
{
do
{
s=s.substring(0,s.length-1);
}while(s.substring(s.length,s.length-1)==" ")
}
return s;
}
//是否是数字字符
function chkNum(ctr)
{
var strTemp="0123456789";
var numStr=ctr;
var j=strTemp.indexOf(numStr);
if(j==-1)
return false;
else
return true;
}
//打开新窗口
function openwin(url,width_,height_,left_,top_)
{
window.open (url, "newwindow", "height="+height_+", width="+width_+",left="+left_+",top="+top_+",toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no")
}
//转换成单精度浮点数
function tofloat(value)
{
value = Math.round(parseFloat(value)*100)/100;
if(value.toString().indexOf(".")<0)
value = value.toString()+".00";
return value;
}
