javascript常用小函数总结

这是写给自己看的总结。

1.动态改变iframe父页面的大小

function changeHeight(){  //动态改变  自己及父页面的大小 ***
	var hg=$("#searchResult").get(0).offsetHeight; //只有这样才能得到 offset

	if (hg < 400) hg = 400;
	$("#searchResult").height(hg);
	$("body").height(200+hg);  //改变自己的高度
	parent.parent.chgheight();//非常重要的一个方法,及其好用。  ****************
	//调用使用iframe的页面的方法
}

//下面这些函数,是在使用iframe的页面中
function chgheight(){ //查询的时候调用它,这样就可以改变大小。
	SetCwinHeight(document.getElementById("framepage"));
}
function SetCwinHeight(obj) { //iframe 达到给定大小,多浏览器兼容。
	var cwin = obj;
	if (document.getElementById) {
		if (cwin && !window.opera) {
			if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
				cwin.height = cwin.contentDocument.body.offsetHeight + 20; //FF NS 
			else if (cwin.Document && cwin.Document.body.scrollHeight)
				cwin.height = cwin.Document.body.scrollHeight + 10; //IE 
		}
		else {
			if (cwin.contentWindow.document && 
<span style="white-space:pre">					</span>cwin.contentWindow.document.body.scrollHeight)
				cwin.height = cwin.contentWindow.document.body.scrollHeight; //Opera 
		}
	}
}

2.时间Date处理及比较两个时间的大小

//将2012-1-1类型的字符串转化为时间
function strToDate(str){
	str = str.replace(/-/g,"/");
	return new Date(str);
}
//下面这个是与当前时间的比较
var now = new Date(),dt;
now=now.getTime();
dt=(buildings[i].bbegintime.split('T'))[0];//这是ASP 从数据库转化为datetime的时候中间夹了 T
dt=strToDate(dt).getTime();
//然后可以比较 dt now

3.指定数字的长度,不够的前面补零

function padLeft(str, lenght) {
	str+="";
	if (str.length >= lenght)
		return str;
	else
		return padLeft("0" + str, lenght);
}

4.为js类加一些类似JAVA,C#的常用的方法

Array.prototype.each = function (fn) {
    return this.length ? [fn(this.slice(0, 1))].concat(this.slice(1).each(fn)) : [];
};
Array.prototype.contains = function (item) {
    return RegExp(item).test(this);
}
String.prototype.trim = function () {

    return this.replace(/[(^\s+)(\s+$)]/g, ""); //會把字符串中間的空白符也去掉 
    //return this.replace(/^\s+|\s+$/g,""); //
    //return this.replace(/^\s+/g,"").replace(/\s+$/g,"");
}

5.js获取url中的参数

function GetRequest() {
   var url = location.search; //获取url中"?"符后的字串
   var theRequest = new Object();
   if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for(var i = 0; i < strs.length; i ++) {
         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
      }
   }
   return theRequest;
}

var Request = new Object();
Request = GetRequest();
modifysno=Request['sno'];

6.判断是否为正整数

function isPositiveNum(s){//是否为正整数
	var re = /^[0-9]*[1-9][0-9]*$/ ;
	return re.test(s)
}

判断是否为正实数。

function jdmoney(money){
	var t=/^\d+(\.\d+)?$/;
	return t.test(money)
}

判断是否为数字

bhomenum=parseInt(bhomenum);
if(isNaN(parseInt(bhomenum))){}

7.页面跳转

function ow(owurl){  	
	owurl="buildinginfo.aspx?bno="+owurl;
	window.location.href=owurl;
} 

8.获得鼠标的位置,相对于整个页面的(穿个参数event)

function mousePosition(ev){ 
    if(ev.pageX || ev.pageY){ 
        return {x:ev.pageX, y:ev.pageY}; 
    } 
    return { 
        x:ev.clientX +document.body.scrollLeft - document.body.clientLeft, 
        y:ev.clientY +document.body.scrollTop - document.body.clientTop
    }; 
}

9.动态增加div的高度

function changeHeight(id){
    /*javascript 版
     * var div=document.getElementById(id);
     */
    //var $div=$("#"+id);
    var div=document.getElementById(id);
    var ht=div.offsetHeight;
    //alert(ht);
    div.style.height=ht;
}

10.操作表单

var fm= document.getElementById("fm");     
for (i=0; i<fm.length; i++){  
	if (fm[i].type=="checkbox" && fm[i].checked){      
	<span style="white-space:pre">	</span>var v=fm[i].value;          
	}  
}    





转载请标明出处


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值