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后边添加一个随机数。
分享到:
相关推荐
里面包含6个JS函数,有获取设备类型、获取url参数、格式化时间戳、复制到粘贴板、去除首位空格、获取cookie。可供学习使用
do.1.21.js,常用js的封装,使用了JSDuck文档生成工具,生成了说明文档。
常用js代码公用方法
这是个人整理的javascript的一些常用方法代码块!里面有ajax请求,浏览器的判断等一些常用代码。
最常用的javascript方法与技巧源码总结
大小写转换 字符串处理 日期格式 流处理 常用正则表达式 常用js
javascript常用对象及方法 javascript常用对象及方法
javascript常用方法javascript常用方法javascript常用方法javascript常用方法javascript常用方法
js常用方法 欢迎大家下载!里面的内容很多,主要有js的常用鼠标事件的方法!喜欢js的朋友可以下载!很全面的!
js-常用方法列表 js-常用方法列表
包括:图片处理特效、文字特效、常用js方法。。。可以作为资料工具用
这个是个人总结整理的javascript 常用方法,居家旅行敲代码的必备神器。
本文主要针对Javascript中字符串相关常用的使用方法进行了总结
javascript常用方法,个人整理.目前还不是很完善,先与大家分享一下。
JS常用的属性和方法,数组的属性:length
【1】删除对象中值为''或null或undefined或[]的所有属性,标准化对象 【2】无序对象根据key从小到大转换成有序数组对象 【3】通过key遍历对象,并通过最小属性和最大属性,获取COG统计信息中的最小最大值 ...
能用就行
一个常用的js帮助类,里面有一些常用的js方法,可以直接引用,也可以把需要的方法复制到自己的js中