⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 v2.4.js

📁 js封装类 项目中常用js封装 希望对大家有帮助
💻 JS
📖 第 1 页 / 共 5 页
字号:
 o {
   id : id of the created tag, 
   url : String,
   script  : String
  }
 用法: dwScript({id : 'cssrain'  , url :  'js/fl.js' });
 */
function dwScript(o){
  o.id = o.id || "";
  o.charset = o.charset || "utf-8";
  if (o.script && o.script != ""){
    document.write("<script id='" + o.id + "'>" + o.script + "<\/script>");
  } else if (o.url && o.url != ""){
    document.write("<script id='" + o.id + "' src='" + o.url + "' charset='" + o.charset + "'><\/script>");
  } else throw new Error("no script content or url specified");
}


/**
 * 以document.write的方式向页面中写入css
 * @param o {
 *    id : id of the created tag, 
 *    url : String,
 *    styles  : styles text
 *  }
用法:dwCSS({ id :  'cssrain'  , url:'css/default.template.css?'});
 */
function dwCSS(o){
  o.id = o.id || "";
  if (o.url){
    document.write('<link id="' + o.id + '" rel="stylesheet" type="text/css" href="' + o.url + '" />');
  } else if (o.styles){
    document.write('<style id="' + o.id + '" >'+o.styles+'<\/style>');
  } 
}


/**
 * 把字符串中 的 半角 转换为全角。
 用法:
 var a  =  "d'd'd'd()%[]";
 alert(toSafe(a));

 当然可以用于表单的值转换。
 */
function toSafe(str)
{
	var re;
	re = /'/g;
	str = str.replace(re,"'");
	re =/\)/g;
	str = str.replace(re,")");
	re = /\(/g;
	str = str.replace(re,"(");
	re = /%/g;
	str = str.replace(re,"%");
	re = /\[/;
	str = str.replace(re,"[");
	re = /\]/;
	str = str.replace(re,"]");
	return str;
}
function makeToSafe(formName)
{
	var i,form;
	form = eval(formName);
	for(i=0;i<form.elements.length;i++)
	{
		if(form.elements[i].type=="text" || form.elements[i].type=="textarea")
		{
			form.elements[i].value = toSafe(form.elements[i].value);
		}
	}
}


/*
   小数   四舍五入
//Dight 为要转换的数据
//How  要保留的小数位数
 用法:
var k = ForDight(  222.5  ,  0 )  ;
 */
function ForDight(Dight,How) 
{
		Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How); 
		return Dight; 
}

/*
计时器。
用法:
<div id="cssrain">aaa</div>

<SCRIPT >
var test = document.getElementById("cssrain");

//测试一个例子,3秒后隐藏div。
function hideDiv()
{
  test.style.color = "red";
}

var cs = new timerPerActive(3 , hideDiv );
cs.callback();
*/

function timerPerActive(timer,comfunc) //对象 
{ 
this.times=0;//定时器对象 ,初始化为0
this.change=function() 
	{ 
	this.times++; 
	test.innerHTML = this.times;//测试用的
	if(this.times==timer) 
		{ 
		clearInterval(times); 
		this.complete();    //最终要回调的函数
		} 
	} 
this.callback=function()  //每过1秒,调用
	{ 
	var css=this; 
	times=setInterval(function(){css.change();},1000); 
	} 
this.complete=function() //完成后,调用
	{
    comfunc();
    }
} 

    
//   计算日期为当年的第几周     
//   用法:    
//   获取   2008-7-5  为当年的第几周     
//   返回:  28     
// document.write(  weekOfYear(2008 ,   7 ,   5)  );  
      function   weekOfYear(year,   month,   day){     
      //   year       年     
      //   month     月     
      //   day         日     
      //   每周从周日开始     
      var   date1   =   new   Date(year,   0,   1);     
      var   date2   =   new   Date(year,   month-1,   day,   1);     
      var   dayMS   =   24*60*60*1000;     
      var   firstDay   =   (7-date1.getDay())*dayMS;     
      var   weekMS   =   7*dayMS;     
      date1   =   date1.getTime();     
      date2   =   date2.getTime();     
      return   Math.ceil((date2-date1-firstDay)/weekMS)+1;     
      }   

//   通过周数和星期计算日期  
//   获取   2005   年第   37   周的周六的日期   。 (0-6,   0代表周日)       
//   返回:   2005年9月10号     
//   alert(dateFromWeek(2005,   37,   6));   
  function   dateFromWeek(year,   week,   day){     
      //   year       年     
      //   week       周     
      //   day         星期   (0-6,   0代表周日)     
      var   date1   =   new   Date(year,   0,   1);     
      var   dayMS   =   24*60*60*1000;     
      var   firstDay   =   (7-date1.getDay())*dayMS;     
      var   weekMS   =   (week-2)*7*dayMS;     
      var   result   =   date1.getTime()+firstDay+weekMS+day*dayMS;     
      date1.setTime(result);     
      return   date1.toLocaleDateString();     
      }  


/**************
format:
格式化时间。
用法:
yourdate.format("你的日期格式");
例子:
obj0 = new Date("Sun May 04 2008").format("yyyy-MM-dd");
obj1 = new Date().format("yyyy-MM-dd hh:mm:ss");
obj2 = new Date().format("yyyy-MM-dd");
obj3 = new Date().format("yyyy/MM/dd");
obj4 = new Date().format("MM/dd/yyyy");
**************/
Date.prototype.format = function(format)   
{   
   var o = {   
     "M+" : this.getMonth()+1, //month   
     "d+" : this.getDate(),    //day   
     "h+" : this.getHours(),   //hour   
     "m+" : this.getMinutes(), //minute   
     "s+" : this.getSeconds(), //second   
     "q+" : Math.floor((this.getMonth()+3)/3), //quarter   
     "S" : this.getMilliseconds() //millisecond   
   }   
   if(/(y+)/.test(format)) format=format.replace(RegExp.$1,   
     (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
   for(var k in o)if(new RegExp("("+ k +")").test(format))   
     format = format.replace(RegExp.$1,   
       RegExp.$1.length==1 ? o[k] :    
         ("00"+ o[k]).substr((""+ o[k]).length));   
   return format;   
} 

/**************
日期减去天数 得到 第二个日期 。
例子:
data_sub_day("12/23/2002",30)
**************/
function data_sub_day(dd,dadd)
{
//可以加上错误处理
var a = new Date(dd)
a = a.valueOf()
a = a - dadd * 24 * 60 * 60 * 1000
a = new Date(a)
alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")
}


/**************
format:
格式化数字.
例子:
var n = format_number( 123456.45656 , 2 ); // .toFixed(2)也可以实现,不过不兼容FF.
alert(n); 
**************/
function format_number(str,digit)
{
 if(isNaN(str))
 {
  alert("您传入的值不是数字!");
  return 0;
 }
 else if(Math.round(digit)!=digit)
 {
  alert("您输入的小数位数不是整数!");
  return 0;
 }
 else 
  return Math.round(parseFloat(str)*Math.pow(10,digit))/Math.pow(10,digit);
} 



/**************
 * 得到单选框选中的值。
 * 用法:
 *<input type="radio"  value="1" name="cssrain"/>
 *<input type="radio"  value="2" name="cssrain" checked/>
 *<input type="radio"  value="3" name="cssrain"/>
 *<input type="button" onclick="alert(getRadioValue('cssrain'))" value="test"/>
**************/
function getRadioValue(radioName){
	var obj=document.getElementsByName(radioName);
	for(var i=0;i<obj.length;i++){
		if(obj[i].checked){
			return obj[i].value;
		}
	}
} 

/**************
 * 复选框全选/不选/反选
 * 用法:
<form id="form_a">
<input type="checkbox"  value="1" name="a"/>
<input type="checkbox"  value="2" name="a" checked/>
<input type="checkbox"  value="3" name="a"/>
<input type="button" value="全选" onclick="checkAll(document.getElementById('form_a'),'all')"/>
<input type="button" value="不选" onclick="checkAll(document.getElementById('form_a'),'none')"/>
<input type="button" value="反选" onclick="checkAll(document.getElementById('form_a'),'')"/>
</form>
**************/
function checkAll(form, sel) {
	for (i = 0, n = form.elements.length; i < n; i++) {
		if(form.elements[i].type == "checkbox") {
			if(form.elements[i].checked == true) {
				form.elements[i].checked = (sel == "all" ? true : false);
			} else {
				form.elements[i].checked = (sel == "none" ? false : true);
			}
		}
	}
}


/**************
 * 复选框检查是否选中。
 * 如果没一个选中,会返回false.
 * 用法:
 <form id="form_a" name="form_a">
<input type="checkbox"  value="1" name="a"/>
<input type="checkbox"  value="2" name="a" checked/>
<input type="checkbox"  value="3" name="a"/>
<input type="button" value="全选" onclick="alert( SCheckBox('form_a','a') )"/>
</form>
**************/
function SCheckBox(_formName,_checkboxName){
	var selflag = {'checked':0,'cvalues':[]};
	_scheckbox = eval('document.'+_formName+'.'+_checkboxName);
	if(_scheckbox){
		if(eval(_scheckbox.length)){
			for(i=0;i<_scheckbox.length;i++){
				if(_scheckbox[i].checked){
					selflag.checked++;
					selflag.cvalues.push(_scheckbox[i].value);
				}
			};
		}else if(_scheckbox.checked){
			selflag.checked++;
			selflag.cvalues.push(_scheckbox.value);
		}
		if(selflag.checked){
			return selflag;
		}
	}
	return false;
}


/**************
收藏到书签.(兼容IE和FF)。
用法:
<input type="button" value="收藏" onclick="addBookmark('cssrain(前端开发)','http://www.cssrain.cn')"/> 
**************/
function addBookmark(title,url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
		return true;
	}
}

/**************
函数 : 文本框得到与失去焦点 操作。
这个方法经常在文本框搜索的时候出现。
文本里显示 “ 搜索 ”,然后当用户鼠标点击此文本,
文本框内容清空。如果用户没填写内容,那么文本的值又复原。
如果填写了,就显示用户填写的。
 用法:
 <input type="" value="关键字搜索" name="a" onfocus="clearTxt('a','关键字搜索')" onblur="fillTxt('a','关键字搜索')"/>
<input type="text" value="test" name="test" />
**************/
function clearTxt(id,txt) {
  if (document.getElementById(id).value == txt)
    document.getElementById(id).value="" ;
  return ;
}
function fillTxt(id,txt) {
  if ( document.getElementById(id).value == "" )
    document.getElementById(id).value=txt;
  return ;
}
//也可以使用 defaultValue属性 来做:
//例子:
/*
<input type="text"  value="搜索"/>
<input type="text"  value="请输入名称"/>
<SCRIPT LANGUAGE="JavaScript">
 var inputs =document.getElementsByTagName("input");
 for(var i=0;i<inputs.length;i++){
     if (inputs[i].type == "submit") continue;
     if (!inputs[i].defaultValue) continue;
      inputs[i].onfocus = function(){
	      if(this.value == this.defaultValue )
		  {
                 this.value = "";
		  }
	  }   //end of onfocus
	    inputs[i].onblur = function(){
	      if(this.value == "" )
		  {
                this.value = this.defaultValue;
		  }
	  }   //end of onblur 
 }
</SCRIPT>
*/




/**************
函数 : 用来判断鼠标按的是左键还是右键。(兼容IE和ff)
用法:
onmousedown="mouse_keycode(event)"
**************/
function mouse_keycode(event){
    var event=event||window.event;
    var nav=window.navigator.userAgent;
    if (nav.indexOf("MSIE")>=1) //如果浏览器为IE.解释:因为 document.all 是 IE 的特有属性,所以通常用这个方法来判断客户端是否是IE浏览器 ,document.all?1:0; 
  { 
   if(event.button==1){alert("左键")}
   else if(event.button==2){alert("右键")}
  }
  else if(nav.indexOf("Firefox")>=1) ////如果浏览器为Firefox 
  {
   if(event.button==0){alert("左键");}
   else if(event.button==2){alert("右键");}
  }
   else{ //如果浏览器为其他 
    alert("other");
   } 
}


/**************
函数 :触发某个对象的onclick事件。(兼容IE和FF)
用法: 
<input type="button" value="aaa" id="a" onclick=" alert('cssrain') " />
<input type="button" value="触发ID为a的onclick事件" onclick=" handerToClick('a') " />
**************/
function handerToClick(objid){
	var obj=document.getElementById(objid);
	if(document.all){
		obj.fireEvent("onclick");
	}else{
	  	var e=document.createEvent('MouseEvent');
	  	e.initEvent('click',false,false);
	  	obj.dispatchEvent(e);
	}
}


/**************
回车提交。
用法:
<input   type=text   onkeydown="keysubmit()">   
**************/
function keysubmit()
{
    if(event.keyCode==13)
   {
	   form.submit();
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -