JS函数大全 莫名其妙找到的

1 .document.write(""); 输出语句

2 .JS中的注释为//

3 .传统的HTML文档顺序是:document->html->(head,body)

4 .一个浏览器窗口中的DOM顺序是:window->(navigator,screen,history,location,document)

5 .得到表单中元素的名称和值:document.getElementById("表单中元素的ID号").name(或value)

6 .一个小写转大写的JS: document.getElementById("output").value = document.getElementById("input").value.toUpperCase();

7 .JS中的值类型:String,Number,Boolean,Null,Object,Function

8 .JS中的字符型转换成数值型:parseInt(),parseFloat()

9 .JS中的数字转换成字符型:(""+变量)

10 .JS中的取字符串长度是:(length)

11 .JS中的字符与字符相连接使用+号.

12 .JS中的比较操作符有:==等于,!=不等于,>,>=,<.<=

13 .JS中声明变量使用:var来进行声明

14 .JS中的判断语句结构:if(condition){}else{}

15 .JS中的循环结构:for([initial expression];[condition];[upadte expression]) {inside loop}

16 .循环中止的命令是:break

17 .JS中的函数定义:function functionName([parameter],...){statement[s]}

18 .当文件中出现多个form表单时.可以用document.forms[0],document.forms[1]来代替.

19 .窗口:打开窗口window.open(), 关闭一个窗口:window.close(), 窗口本身:self

20 .状态栏的设置:window.status="字符";

21 .弹出提示信息:window.alert("字符");

22 .弹出确认框:window.confirm();

23 .弹出输入提示框:window.prompt();

24 .指定当前显示链接的位置:window.location.href="URL"

25 .取出窗体中的所有表单的数量:document.forms.length

26 .关闭文档的输出流:document.close();

27 .字符串追加连接符:+=

28 .创建一个文档元素:document.createElement(),document.createTextNode()

29 .得到元素的方法:document.getElementById()

30 .设置表单中所有文本型的成员的值为空:

var form = window.document.forms[0]

for (var i = 0; i<form.elements.length;i++){

if (form.elements[i].type == "text"){

form.elements[i].value = "";

}

}

31 .复选按钮在JS中判断是否选中:document.forms[0].checkThis.checked (checked属性代表为是否选中返回TRUE或FALSE)

32 .单选按钮组(单选按钮的名称必须相同):取单选按钮组的长度document.forms[0].groupName.length

33 .单选按钮组判断是否被选中也是用checked.

34 .下拉列表框的值:document.forms[0].selectName.options[n].value (n有时用下拉列表框名称加上.selectedIndex来确定被选中的值)

35 .字符串的定义:var myString = new String("This is lightsword");

36 .字符串转成大写:string.toUpperCase(); 字符串转成小写:string.toLowerCase();

37 .返回字符串2在字符串1中出现的位置:String1.indexOf("String2")!=-1则说明没找到.

38 .取字符串中指定位置的一个字符:StringA.charAt(9);

39 .取出字符串中指定起点和终点的子字符串:stringA.substring(2,6);

40 . 数学函数:Math.PI(返回圆周率),Math.SQRT2

 

(返回开方),Math.max(value1,value2)返回两个数中的最在值, Math.pow(value1,10)返回value1的十次方,Math.round(value1)四舍五入函数,Math.floor (Math.random()*(n+1))返回随机数

41 .定义日期型变量:var today = new Date();

42 .日期函数列表:dateObj.getTime()得到时间,dateObj.getYear()得到年份,dateObj.getFullYear()得到四位的年份,dateObj.getMonth()得到月份,dateObj.getDate()得到日,dateObj.getDay()得到日期几, dateObj.getHours()得到小时,dateObj.getMinutes()得到分,dateObj.getSeconds()得到秒, dateObj.setTime(value)设置时间,dateObj.setYear(val)设置年,dateObj.setMonth(val) 设置月,dateObj.setDate(val)设置日,dateObj.setDay(val)设置星期几,dateObj.setHours设置小时,dateObj.setMinutes(val)设置分,dateObj.setSeconds(val)设置秒 [注意:此日期时间从0开始计]

43 .FRAME的表示方式: [window.]frames[n].ObjFuncVarName,frames["frameName"].ObjFuncVarName,frameName.ObjFuncVarName

44 .parent代表父亲对象,top代表最顶端对象

45 .打开子窗口的父窗口为:opener

46 .表示当前所属的位置:this

47 .当在超链接中调用JS函数时用:(Javascript:)来开头后面加函数名

48 .在老的浏览器中不执行此JS:<!-- //-->

49 .引用一个文件式的JS:<script type="text/Javascript" src="aaa.js"></script>

50 .指定在不支持脚本的浏览器显示的HTML:<noscript></noscript>

51 . 当超链和ONCLICK事件都有时,则老版本的浏览器转向a.html,否则转向b.html.例:<a href="a.html" onclick="location.href='b.html';return false">dfsadf</a>

52 .JS 的内建对象有:Array,Boolean,Date,Error,EvalError,Function,Math,Number,Object, RangeError,ReferenceError,RegExp,String,SyntaxError,TypeError,URIError

53 .JS中的换行:\n

54 . 窗口全屏大小:<script>function fullScreen(){ this.moveTo(0,0);this.outerWidth=screen.availWidth;this.outerHeight=screen.availHeight;}window.maximize=fullScreen;</script>

55 .JS中的all代表其下层的全部元素

 

56 .JS中的焦点顺序:document.getElementByid("表单元素").tabIndex = 1

57 .innerHTML 的值是表单元素的值:如<p id="para">"how are <em>you</em>"</p>,则innerHTML的值就是:how are <em>you</em>

58 .innerTEXT的值和上面的一样,只不过不会把<em>这种标记显示出来.

59 .contentEditable可设置元素是否可被修改,isContentEditable返回是否可修改的状态.

60 .isDisabled判断是否为禁止状态.disabled设置禁止状态

61 .length取得长度,返回整型数值

62 .addBehavior()是一种JS调用的外部函数文件其扩展名为.htc

63 .window.focus()使当前的窗口在所有窗口之前.   onfocus获取焦点

64 .blur()指失去焦点.与FOCUS()相反.   onblur失去焦点

65 .select()指元素为选中状态.

66 .防止用户对文本框中输入文本:onfocus="this.blur()"

67 .取出该元素在页面中出现的数量:document.all.tags("div(或其它HTML标记符)").length

68 .JS中分为两种窗体输出:模态和非模态.window.showModaldialog(),windo

 

w.showModeless()

69 .状态栏文字的设置:window.status='文字',默认的状态栏文字设置:window.defaultStatus = '文字.';

70 .添加到收藏夹:external.AddFavorite("http://www.xrss.cn","jaskdlf");

71 .JS中遇到脚本错误时不做任何操作:window.onerror = doNothing; 指定错误句柄的语法为:window.onerror = handleError;

72 .JS中指定当前打开窗口的父窗口:window.opener,支持opener.opener...的多重继续.

73 .JS中的self指的是当前的窗口

74 .JS中状态栏显示内容:window.status="内容"

75 .JS中的top指的是框架集中最顶层的框架

76 .JS中关闭当前的窗口:window.close();

77 .JS中提出是否确认的框:if(confirm("Are you sure?")){alert("ok");}else{alert("Not Ok");}

78 .JS中的窗口重定向:window.navigate("http://www.sina.com.cn");

79 .JS中的打印:window.print()

80 .JS中的提示输入框:window.prompt("message","defaultReply");

81 .JS中的窗口滚动条:window.scroll(x,y)

82 .JS中的窗口滚动到位置:window.scrollby

83 .JS中设置时间间隔:setInterval("expr",msecDelay)或setInterval(funcRef,msecDelay)或setTimeout

84 .JS中的模态显示在IE4+行,在NN中不行:showModalDialog("URL"[,arguments][,features]);

85 .JS 中的退出之前使用的句柄:function verifyClose(){event.returnValue="we really like you and hope you will stay longer.";}} window.onbeforeunload=verifyClose;

86 .当窗体第一次调用时使用的文件句柄:onload()

87 .当窗体关闭时调用的文件句柄:onunload()

88 .window.location的属性: protocol(http:),hostname(www.example.com),port(80),host(www.example.com:80),pathname("/a/a.html"),hash("#giantGizmo",指跳转到相应的锚记),href(全部的信息)

89 .window.location.reload()刷新当前页面.

89 -1.parent.location.reload()刷新父亲对象(用于框架)

89 -2.opener.location.reload()刷新父窗口对象(用于单开窗口)

89 -3.top.location.reload()刷新最顶端对象(用于多开窗口)

90 .window.history.back()返回上一页,window.history.forward()返回下一页,window.history.go(返回第几页,也可以使用访问过的URL)

91 .document.write()不换行的输出,document.writeln()换行输出

92 .document.body.noWrap=true;防止链接文字折行.

93 .变量名.charAt(第几位),取该变量的第几位的字符.

94 ."abc".charCodeAt(第几个),返回第几个字符的ASCii码值.

95 .字符串连接:string.concat(string2),或用+=进行连接

96 .变量.indexOf("字符",起始位置),返回第一个出现的位置(从0开始计算)

97 .string.lastIndexOf(searchString[,startIndex])最后一次出现的位置.

98 .string.match(regExpression),判断字符是否匹配.

99 .string.replace(regExpression,replaceString)替换现有字符串.

100 .string.split(分隔符)返回一个数组存储值.

101 .string.substr(start[,length])取从第几位到指定长度的字符串.

102 .string.toLowerCase()使字符串全部变为小写.

103 .string.toUpperCase()使全

 

部字符变为大写.

104 .parseInt(string[,radix(代表进制)])强制转换成整型.

105 .parseFloat(string[,radix])强制转换成浮点型.

106 .isNaN(变量):测试是否为数值型.

107 .定义常量的关键字:const,定义变量的关键字:var

108 .push,添加数组元素,splice,删除数组元素,splice还可以进行替换元素splice(1,2),1为位置,2为要删除的个数。

109 获得 星期的日期:

if(new Date().getDay()==0) week="周日"

if(new Date().getDay()==1) week="周一"

if(new Date().getDay()==2) week="周二"

if(new Date().getDay()==3) week="周三"

if(new Date().getDay()==4) week="周四"

if(new Date().getDay()==5) week="周五"

if(new Date().getDay()==6) week="周六"

document.write((new Date().getMonth()+1)+"月"+new Date().getDate()+"日 "+week);

 

var d = new Date();

var vYear = d.getFullYear();

var vMon = (d.getMonth()+1)<10 ? "0" + (d.getMonth()+1) : (d.getMonth()+1);

var vDay = d.getDate()<10 ? "0" + d.getDate() : d.getDate();

var vHour = d.getHours()<10 ? "0" + d.getHours() : d.getHours();

var vMin1 = d.getMinutes()<10 ? "0" + d.getMinutes() : d.getMinutes();

var vMin = (d.getMinutes()+5)<10 ? "0" + (d.getMinutes()+5) : (d.getMinutes()+5);

var vSec = d.getSeconds()<10 ? "0" + d.getSeconds() : d.getSeconds();

var now_date = vYear + "-" + vMon + "-" + vDay +" " + vHour + ":"+ vMin1 + ":" + vSec;

 

110.escape 字符转换成所有计算机都可以使用的东东

111.unescape 将转换过的字符转换为正常的字符

112.jmask.dataQueue.splice(0,1); 从数组dataQueue去掉一个元素,位置是0

---------------

缓存解决方法:

self.location("mon.html");作用重新导入页面

---------------------------------------------

播放器最大化:

videoPlayer 为html里某个元素的ID

<script for="videoPlayer" event="onmouseup()">

if(videoPlayer.playState==3)

{

videoPlayer.fullScreen = 'true';

}

 

</script>

----------------------------

得到某个元素的高度

this.oHtmlObj.all('tip').style.top = this.calculateOffsetTop(this.oContainer.oHtmlObj);

 

CheckBoxLable.prototype.calculateOffsetTop = function(field){

return this.calculateOffset(field, "offsetTop");

}

 

CheckBoxLable.prototype.calculateOffset = function(field, attr){

 

var offset = 0;

while(field) {

offset += field[attr];

field = field.offsetParent;

}

//alert("offset="+offset);

return offset;

}

------------------------------------

动态加滚动条:

<div style="width:195px;height:373px;overflow: auto">

</div>

隐藏x轴滚动条:

overflow-x: scroll

overflow-x: hidden

overflow-x: auto !important

 

style="width:190px;height:270px;overflow:scroll;overflow-x: auto;overflow-x:hidden;overflow-y:auto"

-------------------------------------------

//获得键盘事件:向上:window.event.keyCode==38 ,向下window.event.keyCode==40,回车key

 

Code=13 TAB键 keyCode=9

 

function document.onkeydown(){

if ((event.keyCode==38)){

 

}

if ((event.keyCode==40)){

 

}

}

-------------------------------

如何从一个日期中减掉几个小时

<script language = javascript><

var date = new date();

var date = new date(date.utc(date.getyear(),date.getmonth(),date.getdate(),date.gethours(),date.getminutes(),date.getseconds()) - 5*60*60*1000);

document.write(date);

//-script>

-------------

处理时间的例子:

1。解决2000问题

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

2。检查日期是否合法

// 当输入参数为isdate(dd,mm,ccyy)时,表示要检查年,月,日

// 当输入参数为isdate(dd,mm) 表示默认年为当前年

// 当输入参数为isdate(dd) 表示默认年,月为当前年月

// 注意输入月份保证在1-12以内。

 

function isdate (day,month,year) {

var today = new date();

year = ((!year) ? y2k(today.getyear())year);

month = ((!month) ? today.getmonth():month-1);

if (!day) return false

var test = new date(year,month,day);

if ( (y2k(test.getyear()) == year) &&

(month == test.getmonth()) &&

(day == test.getdate()) )

return true;

else

return false

}

 

以下是调用例子:

if (isdate(31,2,1997))

document.write("valid");

else

document.write("invalid");

 

3。如何判断两个日期中的间隔天数

function dayselapsed(date1,date2) {

var difference = date.utc(date1.getyear(),date1.getmonth(),date1.getdate(),0,0,0)

- date.utc(date2.getyear(),date2.getmonth(),date2.getdate(),0,0,0);

return difference/1000/60/60/24;

}

 

4。如何将一个下拉列表框中的月份传递到另一页

<form>

<select name="selectname">

<option>january

<option>february

<option>march

<option>april

<option>may

<option>june

<option>july

<option>august

<option>spetember

<option>october

<option>november

<option>december

</select>

<input type="button" value="go" onclick="window.location.href = nextpage.html? +

this.form.selectname.options[this.form.selectname.selectedindex].text">

</form>

 

在nextpage.html中加入下面的代码

<form name="formname"><input type="text" name="textname"><form>

<script language="javascript"><

document.formname.textname.value = location.search.substring(1);

//-script>

 

或则:

<script language="javascript"><

document.write("<form><input type=text ");

document.write("value="location.search.substring(1)+"form>")

//-script>

 

5。如何将一个字符串中的时间和当前时间做比较

<script language="javascript">

/*其中的日期字符串可有以下格式:

格式 1 : 19970529

格式 2 : 970529

格式 3 : 29/05/1997

格式 4 : 29/05/97

输入参数datetype是1到4的数字,表示使用哪种格式.

*/

<!-

function isittoday(datestring,datetype) {

var now = new date();

var today = new date(now.getyear

 

(),now.getmonth(),now.getdate())

 

if (datetype == 1)

var date = new date(datestring.substring(0,4),

datestring.substring(4,6)-1,

datestring.substring(6,8));

else if (datetype == 2)

var date = new date(datestring.substring(0,2),

datestring.substring(2,4)-1,

datestring.substring(4,6));

else if (datetype == 3)

var date = new date(datestring.substring(6,10),

datestring.substring(3,5)-1,

datestring.substring(0,2));

else if (datetype == 4)

var date = new date(datestring.substring(6,8),

datestring.substring(3,5)-1,

datestring.substring(0,2));

else

return false;

 

if (date.tostring() == today.tostring())

return true;

else

return false;

}

调用的例子如下:

if (isittoday("19970529",1)) alert(true); else alert(false);

if (isittoday("970529",2)) alert(true); else alert(false);

if (isittoday("29/05/1997",3)) alert(true); else alert(false);

if (isittoday("02/06/97",4)) alert(true); else alert(false);

//-

</script>

 

6。如何根据一个人的生日计算他的岁数

<script language="javascript"><

/*其中的日期字符串可有以下格式:

格式 1 : 19970529

格式 2 : 970529

格式 3 : 29/05/1997

格式 4 : 29/05/97

输入参数datetype是1到4的数字,表示使用哪种格式.

*/

function getage(datestring,datetype) {

var now = new date();

var today = new date(now.getyear(),now.getmonth(),now.getdate())

var yearnow = now.getyear();

var monthnow = now.getmonth();

var datenow = now.getdate();

 

if (datetype == 1)

var dob = new date(datestring.substring(0,4),

datestring.substring(4,6)-1,

datestring.substring(6,8));

else if (datetype == 2)

var dob = new date(datestring.substring(0,2),

datestring.substring(2,4)-1,

datestring.substring(4,6));

else if (datetype == 3)

var dob = new date(datestring.substring(6,10),

datestring.substring(3,5)-1,

datestring.substring(0,2));

else if (datetype == 4)

var dob = new date(datestring.substring(6,8),

datestring.substring(3,5)-1,

datestring.substring(0,2));

else

return ;

 

var yeardob = dob.getyear();

var monthdob = dob.getmonth();

var datedob = dob.getdate();

 

yearage = yearnow - yeardob;

 

if (monthnow > monthdob)

var monthage = monthnow - monthdob;

else {

yearage--;

var monthage = 12 + monthnow -monthdob;

}

if (datenow > datedob)

var dateage = datenow - d

 

atedob;

else {

monthage--;

var dateage = 31 + datenow - datedob;

}

 

return yearage + years + monthage + months + dateage + days;

}

调用例子

document.write(getage("19650104",1)+br>)

document.write(getage("650104",2)+br>)

document.write(getage("04/01/1965",3)+br>)

document.write(getage("04/01/65",4)+br>)

//-script>

 

7。如何使用下面的格式dd/mm/yy在网页中显示日期

<script language = javascript>

<!-

var date = new date();

var d = date.getdate();

var day = (d < 10) ? 0 + d : d;

var m = date.getmonth() + 1;

var month = (m < 10) ? 0 + m : m;

var yy = date.getyear();

var year = (yy < 1000) ? yy + 1900 : yy;

 

document.write(day + "/" + month + "/" + year);

//-

</script>

 

8。如何使用下面的格式date month year在网页中显示日期

<script language = javascript>

<!-

function makearray() {

for (i = 0; i<makearray.arguments.length; i++)

this[i + 1] = makearray.arguments[i];

}

 

var months = new makearray(january,february,march,

april,may,june,july,august,september,

october,november,december);

 

var date = new date();

var day = date.getdate();

var month = date.getmonth() + 1;

var yy = date.getyear();

var year = (yy < 1000) ? yy + 1900 : yy;

 

document.write(day + " " + months[month] + " " + year);

//-

</script>

 

9.如何让我的网页的最近更新日期更易读

<script language = javascript><

function makearray0() {

for (i = 0; i<makearray0.arguments.length; i++)

this[i] = makearray0.arguments[i];

}

var days = new makearray0("sunday","monday","tuesday","wednesday",

"thursday","friday","saturday");

var months = new makearray0(january,february,march,

april,may,june,july,august,september,

october,november,december);

function nths(day) {

if (day == 1 || day == 21 || day == 31)

return st;

else

if (day == 2 || day == 22)

return nd;

if (day == 3 || day == 23)

return rd;

else return th;

}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

var last = document.lastmodified;

var date = new date(last);

document.write("last updated on " + days[date.getday()] + +

date.getdate() + nths(date.getdate()) + " " +

months[date.getmonth()] + ", " +

(y2k(date.getyear()) + "."

//-script>

 

10。如何显示到某个特定日期的倒记时

<script language="javascript"><

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

 

function timetilldate(whenday,whenmonth,whenyear) {

var now = new date();

var thisday = now.getdate(), thismonth = now.getmonth() + 1, thisyear = y2k(now.getyear())

var yearsdifference = whenyear - thisyear, monthsdifference = 0, daysdifference = 0, string = ;

 

if (whenmonth >= thismonth) monthsdifference = whenmonth - thismonth;

else { yearsdifference--; monthsdifference = whenmonth + 12 - thismo

 

nth; }

 

if (whenday >= thisday)daysdifference = whenday - thisday;

else {

if (monthsdifference > 0) monthsdifference--;

else { yearsdifference--; monthsdifference+=11; }

daysdifference = whenday + 31 - thisday;

}

 

if (yearsdifference < 0) return ;

 

if ((yearsdifference == 0) && (monthsdifference == 0) && (daysdifference == 0))

return ;

 

if (yearsdifference > 0) {

string = yearsdifference + year;

if (yearsdifference > 1) string += s;

string += ;

}

 

if (monthsdifference > 0) {

string += monthsdifference + month;

if (monthsdifference > 1) string += s;

string += ;

}

if (daysdifference > 0) {

string += daysdifference + day;

if (daysdifference > 1) string += s;

string += ;

}

 

var difference = date.utc(now.getyear(),now.getmonth(),now.getdate(),now.gethours(),now.getminutes(),now.getseconds()) -

date.utc(now.getyear(),now.getmonth(),now.getdate(),0,0,0);

 

difference = 1000*60*60*24 - difference;

 

var hoursdifference = math.floor(difference/1000/60/60);

difference = difference - hoursdifference*1000*60*60

var minutesdifference = math.floor(difference/1000/60);

difference = difference - minutesdifference*1000*60

var secondsdifference = math.floor(difference/1000);

 

if (hoursdifference > 0) {

string += hoursdifference + hour;

if (hoursdifference > 1) string +=s;

string += ;

}

 

if (minutesdifference > 0) {

string += minutesdifference + minute;

if (minutesdifference > 1) string +=s;

string += ;

}

 

if (secondsdifference > 0) {

string += secondsdifference + second;

if (secondsdifference > 1) string +=s;

string += ;

}

return string;

}

 

调用例子,例如现在到31/12/1999还有多久。

document.write(timetilldate(31,12,1999));

//-script>

 

11。如何从一个日期中减掉几个小时

<script language = javascript><

var date = new date();

var date = new date(date.utc(date.getyear(),date.getmonth(),date.getdate(),date.gethours(),date.getminutes(),date.getseconds()) - 5*60*60*1000);

document.write(date);

//-script>

 

12。如何在一个日期中增加几个月后并能够正确显示出来

<script language="javascript"><

function makearray() {

for (i = 0; i<makearray.arguments.length; i++)

this[i + 1] = makearray.arguments[i];

}

var months = new makearray(january,february,march,april,

may,june,july,august,september,

october,november,december);

function nths(day) {

if (day == 1 || day == 21 || day == 31) return st;

else if (day == 2 || day == 22) return nd;

else if (day == 3 || day == 23) return rd;

else return th;

}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function

 

monthsahead(noofmonths) {

var today = new date();

var date = new date(today.getyear(),today.getmonth() + noofmonths,today.getdate(),today.gethours(),today.getminutes(),today.getseconds())

return date.getdate() + nths(date.getdate()) + + months[date.getmonth() + 1] + + y2k(date.getyear())

}

 

调用例子:

document.write(monthsahead(6));

//-script>

posted @ 2017-08-05 21:35  钢铁小坦克  阅读(389)  评论(0编辑  收藏  举报