This article mainly introduces the writing method of js namespace, and analyzes the writing method of JavaScript namespace in the form of a complete example. It has certain reference value. Friends in need can refer to it.
This article analyzes the writing method of js namespace with examples. I share it with you for your reference. The details are as follows:
I have known this writing method for a long time. I have been avoiding it because the foundation Object-oriented is not solid enough, but this method is still necessary for the entire site. Will
html part:
<p id="p1">111</p> <p id="p2">现实</p> <p id="p3">层</p> <p class="tab"> <ul class="tab_nav clearfix"> <li class="active">1</li> <li>2</li> <li>3</li> </ul> <p class="tab_main"> <p style="display: block">内容1</p> <p>内容2</p> <p>内容3</p> </p> </p>
css style:
#p1{width: 100px;height: 100px;background: #ccc;}
#p2{width:100px;height: 20px;background: red;}
#p3{width: 300px;height: 200px;border: 1px solid #ccc;position: absolute;;margin-left: -150px;margin-top:-100px;left:50%;top: 50%;display: none;}
li{width: 100px;float: left;background: #ccc;}
.active{background: red;}
.tab_main{display: none;}
.clearfix:after{clear: both;display: table;content:'';}
.cleafix{zoom:1;}js code:
var namespace={
int:function(){
this.hide.hideFun();
this.show.showFun();
this.tab.tabFun();
}
};
namespace.hide={
hideBtn:$('#p1'),
hideFun:function() {
var that=this;
var a=this.hideBtn;
a.click(function() {
$(this).hide();
});
}
};
namespace.show={
showBtn:$('#p2'),
showBox:$('#p3'),
showFun:function(){
var that=this;
var a=this.showBtn;
var b=this.showBox;
a.click(function(event) {
b.show();
});
}
}
namespace.tab={
tabBtn:$('.tab_nav li'),
tabCon:$('.tab_main p'),
tabFun:function(){
var that=this;
var a=this.tabBtn;
var b=this.tabCon;
a.click(function() {
$(this).addClass('active').siblings().removeClass('active');
b.eq($(this).index()).show().siblings().hide();
});
}
}
namespace.int();The above is the detailed content of js namespace writing example code. For more information, please follow other related articles on the PHP Chinese website!