`

常用js

 
阅读更多

1、按比例缩放图片
ImgD:图片的Node
iwidth:缩放后的宽度
iheight:缩放后的高度

Java代码  收藏代码
  1. function DrawImage(ImgD,iwidth,iheight){   
  2. var flag=false ;   
  3.  var image=new  Image();   
  4.  var iwidth = iwidth;  //定义允许图片宽度    
  5.  var iheight = iheight;  //定义允许图片高度    
  6.  image.src=ImgD.src;   
  7.  if (image.width> 0  && image.height> 0 ){   
  8.  flag=true ;   
  9.  if (image.width/image.height>= iwidth/iheight){   
  10.   if (image.width>iwidth){     
  11.   ImgD.width=iwidth;   
  12.   ImgD.height=(image.height*iwidth)/image.width;   
  13.   }else {   
  14.   ImgD.width=image.width;     
  15.   ImgD.height=image.height;   
  16.   }   
  17.   }   
  18.  else {   
  19.   if (image.height>iheight){     
  20.   ImgD.height=iheight;   
  21.   ImgD.width=(image.width*iheight)/image.height;     
  22.   }else {   
  23.   ImgD.width=image.width;     
  24.   ImgD.height=image.height;   
  25.   }    
  26.   }   
  27.  }   
  28. }    


2、删除空格

Java代码  收藏代码
  1. //删除左右两端的空格   
  2. function trim(str){  
  3.  return  str.replace(/(^\s*)|(\s*$)/g,  "" );  
  4. }  
  5. //删除左边的空格   
  6. function ltrim(str){  
  7.  return  str.replace(/(^\s*)/g, "" );  
  8. }  
  9. //删除右边的空格   
  10. function rtrim(str){  
  11.  return  str.replace(/(\s*$)/g, "" );  
  12. }  


3、删除回车符号

Java代码  收藏代码
  1. function trimEnter(str){  
  2.     return  str.replace(/\n\r/gi, "" );  
  3. }  
  4. 注:2 3 均是Js中正则表达式的应用。  
  5. js中的replace()方法只能够替换掉出现该字符的第一位置处,所以可使用正则表达式来实现。  


4、检测checkbox是否选中及可选中个数
allow=0,则不限制选中的个数

Java代码  收藏代码
  1. function chkCheckbox(checkboxName,allow){  
  2.     var selectCount=0 ;  
  3.     var names=document.getElementsByName(checkboxName);  
  4.     for (var i= 0 ;i<names.length;i++)  
  5.     {  
  6.         var box=names[i];  
  7.         if (box.type== 'checkbox' ){  
  8.             if (box.checked== true ){  
  9.                 selectCount++;  
  10.                 }  
  11.         }  
  12.     }  
  13.     if (selectCount== 0 ){  
  14.         alert("请选择记录!" );  
  15.         return ;  
  16.     }else {  
  17.         if (allow> 0 ){  
  18.             if (selectCount>allow){  
  19.                 alert("只允许选择" +allow+ "条记录,请重新选择!" );  
  20.                 return ;  
  21.             }  
  22.         }  
  23.     }  
  24.     return   true ;  
  25. }  


5、弹出页面

Java代码  收藏代码
  1. function openWin(openUrl,width_1,height_1){  
  2.     var left_1=(screen.width-width_1)/2 ;  
  3.     var top_1=(screen.Height-height_1)/2 ;  
  4.     var paramcon="?" ;  
  5.     if (openUrl.indexOf( "?" )> 0 ) paramcon= "&" ;  
  6.     else  paramcon= "?" ;  
  7.     window.open(openUrl+paramcon+"random=" +Math.random(), "newwindow" , "height=" +height_1+ ", width=" +width_1+ ", top=" +top_1+ ", left=" +left_1+ ", toolbar=mo,titlebar=yes, menubar=no, scrollbars=yes, resizable=yes,location=no, status=yes" );  
  8. }  
  9. function openWinNo(openUrl,width_1,height_1){  
  10.     var left_1=(screen.width-width_1)/2 ;  
  11.     var top_1=(screen.Height-height_1)/2 ;  
  12.     var paramcon="?" ;  
  13.     if (openUrl.indexOf( "?" )> 0 ) paramcon= "&" ;  
  14.     else  paramcon= "?" ;  
  15.     window.open(openUrl+paramcon+"random=" +Math.random(), "newwindow" , "height=" +height_1+ ", width=" +width_1+ ", top=" +top_1+ ", left=" +left_1+ ", toolbar=no,titlebar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no" );  
  16. }  


注:window.open(url,target,attribute);window.open()有三个参数.
url:即页面地址。
target:弹出目标。如果2个弹出页面都写成"newwindow",则只会看到后弹出页面的内容。
attribute:弹出窗口属性。
在被弹出来的页面中可以用window.opener得到上一个页面的window对象,同时也可以使用window里的所有东西。
如上个页面有function setAllValue();则在被弹出来的页面中可以window.opener.setAllValue();
6、模式窗口

Java代码  收藏代码
  1. function getModalDialog(openUrl){  
  2.     var paramcon="?" ;  
  3.     if (openUrl.indexOf( "?" )> 0 ) paramcon= "&" ;  
  4.     else  paramcon= "?" ;  
  5.          var info=new  Object();  
  6.          info.dataInfo="test" ;  
  7.     return  window.showModalDialog(openUrl+paramcon+ "random=" +Math.random(),info, "dialogHeight: 380px; dialogWidth: 500px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: Yes;" );  
  8. }  


在打开来的页面中:
var oMyObject = window.dialogArguments;
可以使用oMyObject.dataInfo得到"test"字符串
window.parent.returnValue="test2";可以设置返回值。
注:showModalDialog(url,object,attribute)有3各参数
url:即要打开的网页地址,可以带参数。同样,url有长度限制,如需传递参数,可使用第二个参数。
object:传递给打开来的页面数据。如上例中的传递方法。
attribute:设置打开来的窗口属性。
模式窗口与window.open()不同。必须关闭模式窗口才能继续在本页面操作。
模式窗口有一定缓存,所以在url后边添加一个随机数。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics