原生js片段

2022-03-21 09:31:01 字數 3279 閱讀 6703

123

4567

891011

1213

1415

1617

1819

2021

2223

2425

2627

2829

3031

3233

3435

36varcookie=else

},// 設定

set:function(name,value,expires),

// 刪除

del:function(name)}};

// demo:

cookie.set("xuanfengge","www.xuanfengge.com",7);

alert(cookie.read("xuanfengge"));

cookie.del("xuanfengge");12

345varastring="hello!";

varbstring=newstring("hello!");

if(astring=="hello!"){}//結果: true

if(astring==bstring){}//結果: true

if(astring===bstring){}//結果: false (兩個對不同,儘管它們的值相同)12

3varmystring="hello everybody.";

// 如果檢索不到會返回-1,檢索到的話返回在該串中的起始位置

if(mystring.indexof("every")>-1){}//結果: true12

varmystring="i is your father.";

varresult=mystring.replace("is","am");//結果: "i am your father."12

3456

78910

1112

//擷取第 6 位開始的字元

varmystring="every good boy does fine.";

varsection=mystring.substring(6);//結果: "good boy does fine."

//擷取第 0 位開始至第 10 位為止的字元

varmystring="every good boy does fine.";

varsection=mystring.substring(0,10);//結果: "every good"

//擷取從第 11 位到倒數第 6 位為止的字元

varmystring="every good boy does fine.";

varsection=mystring.slice(11,-6);//結果: "boy does"

//從第 6 位開始擷取長度為 4 的字元

varmystring="every good boy does fine.";

varsection=mystring.substr(6,4);//結果: "good"12

3456

78910

1112

1314

15functionfucchecklength(strtemp)else

}returnsum;

}// demo:

fucchecklength("qwe");//3

fucchecklength("軒楓");//412

3456

78910

1112

1314

1516

17//合法日期:yyyy-mm-dd,分隔符可以為-/.

string.prototype.isdate=function())([-\/\.])([01]?\d)\2([0-3]?\d)$/;

if(!reg.test(this))returnfalse;

year=regexp.$1.parseint();

month=regexp.$3.parseint()-1;

date=regexp.$4.parseint();

day=newdate(year,month,date);

return((date==day.getdate())&&(month==day.getmonth()));

}// demo:

"1999-12-20".isdate()

// 合法日期:yyyy-mm-dd,分隔符可以為-/12

3456

78910

1112

1314

1516

1718

1920

2122

functiondatediff(date1,date2))([-\/\.])(\d)\2(\d)$/;

reg.test(date1);

year=regexp.$1.parseint();

month=regexp.$3.parseint()-1;

date=regexp.$4.parseint();

day1=newdate(year,month,date);

reg.test(date2);

year=regexp.$1.parseint();

month=regexp.$3.parseint()-1;

date=regexp.$4.parseint();

day2=newdate(year,month,date);

return(day1.gettime()-day2.gettime())/86400000;

}// demo:

// 用以前,先用isdate()判斷一下是否合法日期

// 注意:這個函式的reg和isdate()裡的reg小you不同

// 以天為單位(date1-date2=?),可以用來比較2個日期大小12

3456

78910

1112

functionisempty(obj)

returntrue;

}// demo:

isempty({});//true

isempty();//false

// jq版本

$.isemptyobject({});//true

$.isemptyobject();//false12

3456

78910

object.prototype.length=function()

returncount;

};// demo:

vara=;

alert(a.length());// 5

by 

常用js片段

滑鼠滾輪事件 content on mousewheel dommousescroll function event else if delta 0 驗證碼倒計時 jquery版本 var times 60,timer null send on click function else 1000 常用...

常用JS片段

js判斷只能是數字和小數點 0.不能輸入中文 1 2 1.文字框只能輸入數字 小數點也不能輸入 2.只能輸入數字,能輸小數點.3.數字和小數點方法二 4.只能輸入字母和漢字 5.只能輸入英文本母和數字,不能輸入中文 6.只能輸入數字和英文chun 7.小數點後只能有最多兩位 數字,中文都可輸入 不能...

實用js片段

演算法 加法 add 10,2 12 function add a,b catch f try catch f return e math.pow 10,math.max c,d mul a,e mul b,e e 減法 sub 10,2 8 function sub a,b catch f try...