Javascript 闭包
<!DOCTYPE>
<html>
<head>
<title> new document </title>
</head>
<body>
<div id="source1">
</div>
<div id="source2">
</div>
</body>
<script type="text/javascript">
function $1(id){
var dom =document.getElementById(id);
var html=function(text){
dom.innerHTML=text;
return this;//此处this指$1
}
var css=function(key,value){
dom.style[key]=value;
return this;//此处this指$1
}
//返回一个对象,将函数返回出去
return{html:html,css:css};
}
$1("source1").html("sssss88").css("color","red").css("font-size","40px");
function $2(id){
var dom =document.getElementById(id);
var json={
html:function(text){
dom.innerHTML=text;
return this;//此处this指json
},
css:function(key,value){
dom.style[key]=value;
return this;//此处this指json
}
};
return json;
}
$2("source2").html("sssss88").css("color","red").css("font-size","40px");
</script>
</html>