1、按比例缩放图片
ImgD:图片的Node
iwidth:缩放后的宽度
iheight:缩放后的高度
- function DrawImage(ImgD,iwidth,iheight){
- var flag=false ;
- var image=new Image();
- var iwidth = iwidth; //定义允许图片宽度
- var iheight = iheight; //定义允许图片高度
- image.src=ImgD.src;
- if (image.width> 0 && image.height> 0 ){
- flag=true ;
- if (image.width/image.height>= iwidth/iheight){
- if (image.width>iwidth){
- ImgD.width=iwidth;
- ImgD.height=(image.height*iwidth)/image.width;
- }else {
- ImgD.width=image.width;
- ImgD.height=image.height;
- }
- }
- else {
- if (image.height>iheight){
- ImgD.height=iheight;
- ImgD.width=(image.width*iheight)/image.height;
- }else {
- ImgD.width=image.width;
- ImgD.height=image.height;
- }
- }
- }
- }
2、删除空格
- //删除左右两端的空格
- function trim(str){
- return str.replace(/(^\s*)|(\s*$)/g, "" );
- }
- //删除左边的空格
- function ltrim(str){
- return str.replace(/(^\s*)/g, "" );
- }
- //删除右边的空格
- function rtrim(str){
- return str.replace(/(\s*$)/g, "" );
- }
3、删除回车符号
- function trimEnter(str){
- return str.replace(/\n\r/gi, "" );
- }
- 注:2 、 3 均是Js中正则表达式的应用。
- js中的replace()方法只能够替换掉出现该字符的第一位置处,所以可使用正则表达式来实现。
4、检测checkbox是否选中及可选中个数
allow=0,则不限制选中的个数
- function chkCheckbox(checkboxName,allow){
- var selectCount=0 ;
- var names=document.getElementsByName(checkboxName);
- for (var i= 0 ;i<names.length;i++)
- {
- var box=names[i];
- if (box.type== 'checkbox' ){
- if (box.checked== true ){
- selectCount++;
- }
- }
- }
- if (selectCount== 0 ){
- alert("请选择记录!" );
- return ;
- }else {
- if (allow> 0 ){
- if (selectCount>allow){
- alert("只允许选择" +allow+ "条记录,请重新选择!" );
- return ;
- }
- }
- }
- return true ;
- }
5、弹出页面
- function openWin(openUrl,width_1,height_1){
- var left_1=(screen.width-width_1)/2 ;
- var top_1=(screen.Height-height_1)/2 ;
- var paramcon="?" ;
- if (openUrl.indexOf( "?" )> 0 ) paramcon= "&" ;
- else paramcon= "?" ;
- 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" );
- }
- function openWinNo(openUrl,width_1,height_1){
- var left_1=(screen.width-width_1)/2 ;
- var top_1=(screen.Height-height_1)/2 ;
- var paramcon="?" ;
- if (openUrl.indexOf( "?" )> 0 ) paramcon= "&" ;
- else paramcon= "?" ;
- 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" );
- }
注:window.open(url,target,attribute);window.open()有三个参数.
url:即页面地址。
target:弹出目标。如果2个弹出页面都写成"newwindow",则只会看到后弹出页面的内容。
attribute:弹出窗口属性。
在被弹出来的页面中可以用window.opener得到上一个页面的window对象,同时也可以使用window里的所有东西。
如上个页面有function setAllValue();则在被弹出来的页面中可以window.opener.setAllValue();
6、模式窗口
- function getModalDialog(openUrl){
- var paramcon="?" ;
- if (openUrl.indexOf( "?" )> 0 ) paramcon= "&" ;
- else paramcon= "?" ;
- var info=new Object();
- info.dataInfo="test" ;
- return window.showModalDialog(openUrl+paramcon+ "random=" +Math.random(),info, "dialogHeight: 380px; dialogWidth: 500px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: Yes;" );
- }
在打开来的页面中:
var oMyObject = window.dialogArguments;
可以使用oMyObject.dataInfo得到"test"字符串
window.parent.returnValue="test2";可以设置返回值。
注:showModalDialog(url,object,attribute)有3各参数
url:即要打开的网页地址,可以带参数。同样,url有长度限制,如需传递参数,可使用第二个参数。
object:传递给打开来的页面数据。如上例中的传递方法。
attribute:设置打开来的窗口属性。
模式窗口与window.open()不同。必须关闭模式窗口才能继续在本页面操作。
模式窗口有一定缓存,所以在url后边添加一个随机数。