1、var now=new Date()
新创建的对象自动获取当前日期和时间。如果要根据特定的日期和时间创建日期对象,必须传入表示该日期的毫秒数(即从UTC时间 1970年1月1日午夜至该日期止所经过的毫秒数。) 注意是毫秒数!!!
Date.parse()---接收一个表示日期的字符串参数,然后尝试根据这个字符串返回相应日期的毫秒数。
年/月/日 小时:分:秒 0000/00/00
00:00:00
英文月 日,年 小时:分:秒 美国月 00,0000 00:00:00
var someDate=new Date(Date.parse("May 25,2004"));
等价于 var somDate=new Date("May 25,2004");
Date.UTC()--返回表示日期的毫秒数,参数是0000,00,00,00,00,00,00 从0开始算
var allfives=new Date(Date.UTC(2005,4,5,17,55,55))
2、
toDateString()- 以特定于实现的格式显示星期几、月、日和年
toTimeString()-以特定于实现的格式显示时、分、秒和时区
getTime()-返回毫秒数
setTime()-以毫秒数设置日期
getFullYear/getUTCFullYear/setFullYear 等等
嘿嘿,依据这个可以做个简单的倒计时。
看HTML
<div class="deadline" nowtime="undefined" endtime="2012/4/20 0:00:00">离试用申请结束还有:
<span
class="day"></span>天<span class="hour"></span>小时<span class="minute"></span>分<spanclass="second"></span>秒
</div>
看js
var cd;
var cut = 0;
var now = new Date();
function countdown() {
cut++;
$(".deadline").each(function
() {
var
endTime = Date.parse($(this).attr("endtime"));//设置到期时间毫秒数
var
nowtime = now.getTime();
var
remain = parseInt((endTime - nowtime) / 1000) - cut;//判断剩下的秒数
var
day = parseInt(remain / 3600 / 24);//计算剩下多少天
var
hour = parseInt((remain / 3600) % 24);
var
minute = parseInt((remain / 60) % 60);
var
second = parseInt(remain % 60);
if
(hour < 10) hour = "0" + hour;
if
(minute < 10) minute = "0" + minute;
if
(second < 10) second = "0" + second;
$(this).find(".day").html(day);
$(this).find(".hour").html(hour);
$(this).find(".minute").html(minute);
$(this).find(".second").html(second);
if
(remain <= 0) {
$(this).html("试用申请结束已结束");
}
});
}
cd = setInterval(countdown, 1000);//每隔1秒刷新下
3、每当读取一个基本类型值的时候,后台会创建一个对应的基本包装类型对象,从而可以调用一些方法来操作数据。
var s1="some test";
var s2=s1.Substring(2);
后台自动完成一些列处理:1、创建String类型的实例 2、在实例上调用方法 3、销毁实例
引用类型和基本包装类型的主要区别在于对象的生存期。
用new创建的引用类型实例,在执行流离开当前作用于之前都在内存中。自动创建的基本包装类型对象,只存在于一行代码的执行瞬间,然后被销毁。这我们不能再运行时未基本类型值添加属性和方法。
4、Boolean基本包装类型对象。 记住:布尔表达式中所有对象都会被转换为true!!!
var falseObject = { name: "nike" };
var falseObject2 = { name: "nike2" };
var result = falseObject && falseObject2;
alert(result.name);//nike2
var falseObject3 = new Boolean(false);
var result2 = falseObject3 && true;
alert(result2); //true
var falseObject4 = false;
var result3 = falseObject4 && true;
alert( result3)//fasle
5、indexOf() 和lastIndexOf()--从一个字符串中搜索给定的子字符串,然后返回子字符串的位置,如果没有找到该子字符串,则返回-1。
包含指定的字符。
var stringvalue = "hoeollo world";
document.write("第一次出现o的位置:" + stringvalue.indexOf("o") + "<br />"
+ "最后一次出现o的位置" + stringvalue.lastIndexOf("o")+"<br />"
+ "前6个字符中即" + stringvalue.slice(0, 5) + "中,最后一次出现o的位置" + stringvalue.lastIndexOf("o", 5) + "<br />"
+ "从第6个字符开始" + stringvalue.slice(5) + "中,第一次出现o的位置" + stringvalue.indexOf("o", 5)
);
6、URI[Uniform Resource Identifiers 通用资源标识符]编码方法
encodeURI()主要用于整个URI 而 encodeURIComponent()主要用于对URI中的某一段进行编码。
区别:encodeURI()不会对本身属于URI的特殊字符进行编码,例如冒号、正斜杠、问号和#字号;
而encodeURIComponent()则会对它发现的任何非标准字符进行编码。
decodeURI、decodeURIComponent
Math.max() Match.min()
Math.ceil()向上舍入,Match.floor()向下舍入,Math.round()执行标准舍入。
7、正则表达式
对于入门正则表达式来说,下面这篇文章是不错的。
http://www.regexlab.com/zh/regref.htm
exec()的参数是要应用模式的字符串,然后返回包含第一个匹配项信息的数组,或者在没有匹配想的情况下返回null.
返回数组有两个额外属性:index 和 input。其中index表示匹配项在字符串中的位置,而input表示应用正则表达式的字符串。
对于exec()而言,即使在模式中设置了全局标志(g),它每次也只会返回一个匹配项。
在不设置全局标志的情况下,在同一个字符串上多次调用exec()将始终返回第一个匹配项的信息,而在设置全局标志的情况下,每次调用exec()则都会在字符串中继续查找新匹配项。
注意:IE的JavaScript实现在lastIndex属性上存在偏差,即使在非全局模式下,lastIndex属性每次也会改变。
var text = "cat, bat, sat, fat";
var pattern1 = /.at/;
var matches = pattern1.exec(text);
alert(matches.index);//0
alert(matches[0]);//cat
alert(pattern1.lastIndex);//0
matches = pattern1.exec(text);
alert(matches.index);//0
alert(matches[0]);//cat
alert(pattern1.lastIndex);//0
--------------------------------------------
var text = "cat, bat, sat, fat";
var pattern1 = /.at/g;
var matches = pattern1.exec(text);
alert(matches.index);//0
alert(matches[0]);//cat
alert(pattern1.lastIndex);//3
matches = pattern1.exec(text);
alert(matches.index);//5
alert(matches[0]);//bat
alert(pattern1.lastIndex);//8
正则表达式第二个方法是test(),参数是字符串。在模式与该参数匹配的情况下返回true。
var text = "000-00-0000";
var
pattern = /\d{3}-\d{2}-\d{4}/;
if
(pattern.test(text))
alert("The
pattern was matched");
8、String类型
访问字符串中特定字符:charAt()、charCodeAt() 这两个方法都接收一个参数,即基于0的字符位置。
var stringvalue = "hello world";
alert(stringvalue.charAt(1)); //e
alert(stringvalue.charCodeAt(1)); //101
concat() 用于将一个或多个字符串拼接起来,返回拼接得到的新字符串。
var stringValue = "hello ";
var result = stringValue.concat("world");
alert(result);//"hello world"
alert(stringValue);//hello
match()--返回一个数组,只接受一个参数,一个正则表达式或是RegExp。返回的具体数组内容与模式相关。
下面例子对比下exec.
var someText = "web2.0 12 .net2.0";
var pattern = /(\w+)(\d)\.(\d)/g;
outCome_exec = pattern.exec(someText);
outCome_matc = someText.match(pattern);
alert( outCome_exec+"--"+outCome_matc);//web2.0,web,2,0--web2.0,net2.0
var text = "cat1, bat2, sat3, fat";
var pattern2 = /(.at)\d/;
var pattern3 = /(.at)\d/g;
var matches = text.match(pattern2);
var matches2 = text.match(pattern3);
alert(matches); //cat1,cat
alert(matches2); //cat1,bat2,sat3
search()--参数与match相同,返回字符串中第一个个匹配项的索引,如果没有找到匹配项,返回-1。
var text = "cat,bat,sat,fat";
var pos=text.search(/at/);
alert(pos);//1
replace()--可以接受两个参数:一个是RegExp对象或者是一个字符串(字符串不会被转换成正则表达式);第二个参数可以是一个字符串或者是一个函数。
如果是字符串,只会替换第一个子字符串。用正则,指定全局g。
var text = "cat,bat,sat,fat";
var result = text.replace("at", "ond");
alert(result);//cond,bat,sat,fat
result = text.replace(/.at/g, "ond");
alert(result);//cond,bond,sond,fond
function test() {
for
(var i = 0; i < arguments.length; i++) {16:06 2012/3/31
document.write("第"
+ (i + 1) + "个参数的值:" + arguments[i]+"<br />");
}
}
var reg = new RegExp("\\d", "g");
var str = "abd1afa4sdf";
str.replace(reg, test);
var text = "cat23,bat2,sat5,fat67";
var result = text.replace(/(.at)(\d+)/g, "word($1$2)");
document.write(result); //word(cat23),word(bat2),word(sat5),word(fat67)
参数是函数,在只有一个匹配项(即与模式匹配的字符串)的情况下,传递三个参数:模式的匹配项,模式匹配项在字符中的位置和原始字符串。
function htmlEscape(text) {
return
text.replace(/[<>"&]/g, function (match, pos, originalText) {
document.write(match+"<br
/>");
switch
(match) {
case
"<": return "<";
case
">": return ">";
case
"&": return "&";
case
"\"": return """;
}
});
}
alert(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
split()---基于制定的分隔符将一个字符串分隔成多个字字符串,并将结果放在一个数组中。分隔符可以是字符串,也可以是RegExp对象。第二个参数,用于指定数组的大小。
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");
var colors2 = colorText.split(",", 2);
document.write(colors1 + "--" + colors2); //red,blue,green,yellow--red,blue
localeCompare(),比较字符串返回(负数,0,正数)具体返回数值,依据浏览器定
var stringvalue = "yellow";
alert( stringvalue.localeCompare("brick"));
fromCharCode()---接收字符编码,将他们转换成一个字符串。这个有时候用来进行跨浏览器注入攻击。
alert(String.fromCharCode(104,101,108,108,111));//hello
新创建的对象自动获取当前日期和时间。如果要根据特定的日期和时间创建日期对象,必须传入表示该日期的毫秒数(即从UTC时间 1970年1月1日午夜至该日期止所经过的毫秒数。) 注意是毫秒数!!!
Date.parse()---接收一个表示日期的字符串参数,然后尝试根据这个字符串返回相应日期的毫秒数。
年/月/日 小时:分:秒
英文月 日,年 小时:分:秒 美国月 00,0000 00:00:00
var someDate=new Date(Date.parse("May 25,2004"));
等价于 var somDate=new Date("May 25,2004");
Date.UTC()--返回表示日期的毫秒数,参数是0000,00,00,00,00,00,00 从0开始算
var allfives=new Date(Date.UTC(2005,4,5,17,55,55))
2、
toDateString()- 以特定于实现的格式显示星期几、月、日和年
toTimeString()-以特定于实现的格式显示时、分、秒和时区
getTime()-返回毫秒数
setTime()-以毫秒数设置日期
getFullYear/getUTCFullYear/setFullYear 等等
嘿嘿,依据这个可以做个简单的倒计时。
看HTML
<div class="deadline" nowtime="undefined" endtime="2012/4/20 0:00:00">离试用申请结束还有:
看js
var cd;
var cut = 0;
var now = new Date();
function countdown() {
}
cd = setInterval(countdown, 1000);//每隔1秒刷新下
3、每当读取一个基本类型值的时候,后台会创建一个对应的基本包装类型对象,从而可以调用一些方法来操作数据。
var s1="some test";
var s2=s1.Substring(2);
后台自动完成一些列处理:1、创建String类型的实例 2、在实例上调用方法 3、销毁实例
引用类型和基本包装类型的主要区别在于对象的生存期。
用new创建的引用类型实例,在执行流离开当前作用于之前都在内存中。自动创建的基本包装类型对象,只存在于一行代码的执行瞬间,然后被销毁。这我们不能再运行时未基本类型值添加属性和方法。
4、Boolean基本包装类型对象。 记住:布尔表达式中所有对象都会被转换为true!!!
var falseObject = { name: "nike" };
var falseObject2 = { name: "nike2" };
var result = falseObject && falseObject2;
alert(result.name);//nike2
var falseObject3 = new Boolean(false);
var result2 = falseObject3 && true;
alert(result2); //true
var falseObject4 = false;
var result3 = falseObject4 && true;
alert( result3)//fasle
5、indexOf() 和lastIndexOf()--从一个字符串中搜索给定的子字符串,然后返回子字符串的位置,如果没有找到该子字符串,则返回-1。
包含指定的字符。
var stringvalue = "hoeollo world";
document.write("第一次出现o的位置:" + stringvalue.indexOf("o") + "<br />"
+ "最后一次出现o的位置" + stringvalue.lastIndexOf("o")+"<br />"
+ "前6个字符中即" + stringvalue.slice(0, 5) + "中,最后一次出现o的位置" + stringvalue.lastIndexOf("o", 5) + "<br />"
+ "从第6个字符开始" + stringvalue.slice(5) + "中,第一次出现o的位置" + stringvalue.indexOf("o", 5)
);
6、URI[Uniform Resource Identifiers 通用资源标识符]编码方法
encodeURI()主要用于整个URI 而 encodeURIComponent()主要用于对URI中的某一段进行编码。
区别:encodeURI()不会对本身属于URI的特殊字符进行编码,例如冒号、正斜杠、问号和#字号;
而encodeURIComponent()则会对它发现的任何非标准字符进行编码。
decodeURI、decodeURIComponent
Math.max() Match.min()
Math.ceil()向上舍入,Match.floor()向下舍入,Math.round()执行标准舍入。
7、正则表达式
对于入门正则表达式来说,下面这篇文章是不错的。
http://www.regexlab.com/zh/regref.htm
exec()的参数是要应用模式的字符串,然后返回包含第一个匹配项信息的数组,或者在没有匹配想的情况下返回null.
返回数组有两个额外属性:index 和 input。其中index表示匹配项在字符串中的位置,而input表示应用正则表达式的字符串。
对于exec()而言,即使在模式中设置了全局标志(g),它每次也只会返回一个匹配项。
在不设置全局标志的情况下,在同一个字符串上多次调用exec()将始终返回第一个匹配项的信息,而在设置全局标志的情况下,每次调用exec()则都会在字符串中继续查找新匹配项。
注意:IE的JavaScript实现在lastIndex属性上存在偏差,即使在非全局模式下,lastIndex属性每次也会改变。
var text = "cat, bat, sat, fat";
var pattern1 = /.at/;
var matches = pattern1.exec(text);
alert(matches.index);//0
alert(matches[0]);//cat
alert(pattern1.lastIndex);//0
matches = pattern1.exec(text);
alert(matches.index);//0
alert(matches[0]);//cat
alert(pattern1.lastIndex);//0
--------------------------------------------
var text = "cat, bat, sat, fat";
var pattern1 = /.at/g;
var matches = pattern1.exec(text);
alert(matches.index);//0
alert(matches[0]);//cat
alert(pattern1.lastIndex);//3
matches = pattern1.exec(text);
alert(matches.index);//5
alert(matches[0]);//bat
alert(pattern1.lastIndex);//8
正则表达式第二个方法是test(),参数是字符串。在模式与该参数匹配的情况下返回true。
var text = "000-00-0000";
8、String类型
访问字符串中特定字符:charAt()、charCodeAt() 这两个方法都接收一个参数,即基于0的字符位置。
var stringvalue = "hello world";
alert(stringvalue.charAt(1)); //e
alert(stringvalue.charCodeAt(1)); //101
concat() 用于将一个或多个字符串拼接起来,返回拼接得到的新字符串。
var stringValue = "hello ";
var result = stringValue.concat("world");
alert(result);//"hello world"
alert(stringValue);//hello
match()--返回一个数组,只接受一个参数,一个正则表达式或是RegExp。返回的具体数组内容与模式相关。
下面例子对比下exec.
var someText = "web2.0 12 .net2.0";
var pattern = /(\w+)(\d)\.(\d)/g;
outCome_exec = pattern.exec(someText);
outCome_matc = someText.match(pattern);
alert( outCome_exec+"--"+outCome_matc);//web2.0,web,2,0--web2.0,net2.0
var text = "cat1, bat2, sat3, fat";
var pattern2 = /(.at)\d/;
var pattern3 = /(.at)\d/g;
var matches = text.match(pattern2);
var matches2 = text.match(pattern3);
alert(matches); //cat1,cat
alert(matches2); //cat1,bat2,sat3
search()--参数与match相同,返回字符串中第一个个匹配项的索引,如果没有找到匹配项,返回-1。
var text = "cat,bat,sat,fat";
var pos=text.search(/at/);
alert(pos);//1
replace()--可以接受两个参数:一个是RegExp对象或者是一个字符串(字符串不会被转换成正则表达式);第二个参数可以是一个字符串或者是一个函数。
如果是字符串,只会替换第一个子字符串。用正则,指定全局g。
var text = "cat,bat,sat,fat";
var result = text.replace("at", "ond");
alert(result);//cond,bat,sat,fat
result = text.replace(/.at/g, "ond");
alert(result);//cond,bond,sond,fond
function test() {
}
var reg = new RegExp("\\d", "g");
var str = "abd1afa4sdf";
str.replace(reg, test);
var text = "cat23,bat2,sat5,fat67";
var result = text.replace(/(.at)(\d+)/g, "word($1$2)");
document.write(result); //word(cat23),word(bat2),word(sat5),word(fat67)
参数是函数,在只有一个匹配项(即与模式匹配的字符串)的情况下,传递三个参数:模式的匹配项,模式匹配项在字符中的位置和原始字符串。
function htmlEscape(text) {
}
alert(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
split()---基于制定的分隔符将一个字符串分隔成多个字字符串,并将结果放在一个数组中。分隔符可以是字符串,也可以是RegExp对象。第二个参数,用于指定数组的大小。
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");
var colors2 = colorText.split(",", 2);
document.write(colors1 + "--" + colors2); //red,blue,green,yellow--red,blue
localeCompare(),比较字符串返回(负数,0,正数)具体返回数值,依据浏览器定
var stringvalue = "yellow";
alert( stringvalue.localeCompare("brick"));
fromCharCode()---接收字符编码,将他们转换成一个字符串。这个有时候用来进行跨浏览器注入攻击。
alert(String.fromCharCode(104,101,108,108,111));//hello