常用JS方法

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后边添加一个随机数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值