01 window.location.href http://www.home.com:8080/windows/location/page.html?ver=1.0&id=timlq#love
window.location.origin http://www.home.com:8080
window.location.protocol http:
window.location.host www.home.com
window.location.port 8080
window.location.pathname /windows/location/page.html
window.location.search ?ver=1.0&id=timlq
window.location.hash #love
02 str2=str1.replace() 不影响 返新
03 $dom.click(function(){}).click();
04 $dom.each()返回的还是对象集合
each(function(){}):是回调函数,在回调函数里不能返回结果到回调函数each外面
return true =>continue return false=>break
05 reg.test(str) //true false
reg.exec(str) //array[0]为原字符串,array[i]为匹配在整个被搜索字符串中的位置 /i\B/g.exec("i love china!")=>[0:"i",index:9,input:"i love china!"]
while(res=patt.exec(str) !=null){ patt.lastIndex lastIndex 属性重置 }
str.match(reg) //返回匹配结果数组,null ("1 plus 2 equal 3").match(/\d+/)=>[0:"1",index:0,input:"1 plus 2 equal 3"] ...(/\d/g)=>[1,2,3]
str.search(reg) //index -1
str.replace(reg,"")
str.split(reg) //返回按正则分割的数组 "http://www.baidu.com/".split(/\W/)=>["http", "", "", "www", "baidu", "com", ""]
06 setTimeout:
function test(){
for(var i=0;i<5;i++){
setTimeout(function () {
alert(i);
},100);
}
}
test(); //5 5 5 5 5
// 解决1
function test() {
for(var i=0;i<5;i++){
setTimeout("alert("+i+")",100)
}
}
test(); //1 2 3 4 5
// 解决3
function test() {
for(var i=0;i<5;i++){
(function (i) {
setTimeout(function () {
alert(i);
},100);
})(i);
}
}
test(); //1 2 3 4 5
07 arr.forEach(function(item,i,arr1){ // arr1内容被arr替换
console.log(this) // window = > thisObj
},thisObj)
08 typeof 只能返回 number,boolean,string,function,object(NULL,数组,对象),undefined
判断变量是否存在 if(typeof a != "undefined") if(a) //错误 a(未声明)则报错。
var a=new Array();
if(a instanceof Array) // true a为Array对象的实例
if(a instanceof Object) // true Array为Object对象的实例
09 escape() unescape() 能使所有的计算机上读取该字符串,字符都会被转义序列替换
该方法不会对ASCII字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: * @ - _ + . /
<script type="text/javascript">
var test1="Visit W3School!"
test1=escape(test1)
document.write (test1 + "<br />") //Visit%20W3School%21
test1=unescape(test1)
document.write(test1 + "<br />") //Visit W3School!
</script>
10 //url参数获取
function param(str,name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = str.substr(2).match(reg);
console.log(r)
if (r != null) return unescape(r[2]);
return null;
}
new RegExp("(?|&)" + name + "=([^&]*)(&|$)", "i"); 字符\代表转义 new RegExp("\\w") == /\w/
11 json_encode json_decode PHP中
<?php
$a = array("name"=>"jjaa","age"=>21,"birth"=>"2012-08-04");
$json_a=json_encode($a);
var_dump($json_a);
$b=json_decode($json_a);
var_dump($b);
$c=json_decode($json_a,true);
var_dump($c);
?>
'{"name":"jjaa","age":21,"birth":"2012-08-04"}' (length=45)
object(stdClass)[1166]
public 'name' => string 'jjaa' (length=4)
public 'age' => int 21
public 'birth' => string '2012-08-04' (length=10)
array (size=3)
'name' => string 'jjaa' (length=4)
'age' => int 21
'birth' => string '2012-08-04' (length=10)