Home  >  Article  >  Web Front-end  >  js namespace writing example code

js namespace writing example code

怪我咯
怪我咯Original
2017-07-07 15:09:111407browse

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:&#39;&#39;;}
.cleafix{zoom:1;}

js code:

var namespace={
 int:function(){
  this.hide.hideFun();
  this.show.showFun();
  this.tab.tabFun();
 }
};
namespace.hide={
 hideBtn:$(&#39;#p1&#39;),
 hideFun:function() {
  var that=this;
  var a=this.hideBtn;
  a.click(function() {
   $(this).hide();
  });
 }
};
namespace.show={
 showBtn:$(&#39;#p2&#39;),
 showBox:$(&#39;#p3&#39;),
 showFun:function(){
  var that=this;
  var a=this.showBtn;
  var b=this.showBox;
  a.click(function(event) {
   b.show();
  });
 }
}
namespace.tab={
 tabBtn:$(&#39;.tab_nav li&#39;),
 tabCon:$(&#39;.tab_main p&#39;),
 tabFun:function(){
  var that=this;
  var a=this.tabBtn;
  var b=this.tabCon;
  a.click(function() {
   $(this).addClass(&#39;active&#39;).siblings().removeClass(&#39;active&#39;);
   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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn