JS implementation like jquery basic function Simple selector/Event/attribute

Source: Internet
Author: User

Button style definition <style>.btn{display:inline-block;width:100px;height:20px;color: #fff; font-size:12px; Background-color: #0033dd; text-align:center;line-height:20px;text-decoration:none;border:5px #0000ff outset;}. btn-big-test{width:300px;height:85px;line-height:85px;font-size:25px;} </style>

Based on the basic JS, a simple base selector with element selection supported by id/name/class/tag/object is implemented, and the mechanism of event delegation and removal is realized, and the property operation function and continuous invocation mode are realized. Exception capture

Claim button <a href= "javascript:;" class= "Btn-big-test btn" title= ' You ordered a huge button! ' > Huge test button </a><br/><br/><a href= "javascript:;" id= "Removetest" class= "BTN Grey" > Remove test Event </a>&nbsp;<a href= "javascript:;" id= "Addtest" class= "BTN" > Open test Event </a>


/* Declare Function */$_fn=function (selectname) {this.selectname=selectname;this.obj=this.selectelement (); return this;} Prototype method $_fn.prototype={//Basic selector   No subclass   Continuous pattern selector ' selectelement ': function () {if (typeof  this.selectname ==  ' object ') {return this.selectname;} Else if (This.selectName.indexOf ("#")!=-1) {Return document.getelementbyid (' # ') ,‘‘));} Else if (This.selectName.indexOf (".")! =-1) {return document.getelementsbyclassname ("this.selectName.replace ('. ', ')");} Else if (This.selectName.indexOf ("@")!=-1) {Return document.getelementsbyname (This.selectName.replace ( ‘@‘,‘‘));} Else{return document.getelementsbytagname (This.selectname);}}, ' On ': function (event,callback) {try{$_ eventstack[this.obj]=callback;if  (/.* (firefox| safari| Chrome). */.exec (Window.navigator.userAgent))  {if (this.obj.length>0) {for  (var i=0;i< this.obj.length;i++)  {this.obj[i].addeventlistener ("click", Callback,false);};} Else{this.obj.addEventListener ("click", Callback,false);}} Else{if (this.obj.length>0) {for  (var i=0;i<this.obj.length;i++)  {if (This.obj[i]) this.obj[i ].attachevent= ("onclick", callback);}} Else{this.obj.attachevent= ("onclick", callback);}}} catch (E) {console.error (e);} Return this;},detach:function (Event) {try{if  (/.* (firefox| safari| Chrome). */.exec (Window.navigator.userAgent))  {if (this.obj.length>0) {for  (var i=0;i< this.obj.length;i++)  {this.obj[i].removeeventlistener (event,$_eventstack[this.obj],false);};} Else{this.obj.removeeventlistener (Event,$_eventstack[this.obj],false);}} Else{if (this.obj.length>0) {for  (var i=0;i<this.obj.length;i++)  {if (This.obj[i]) this.obj[i ].detachevent ("on" +event,$_eventstack[this.obj]);}} Else{this.obj.detachevent ("on" +event,$_eventstack[this.obj]);}}} catch (E) {console.error (e);} Return this;},attr:function (Propname,value) {if (!value) {return this.obj.getattribute (propName);} Else{this.obj.setattribute (Value); return this;}}}  window.$_ = function (selectname) {RETURN&NBSP;NEW&NBSP;$_FN (selectname);};/ /Get Smart Object  window.$_eventstack={};//Log event delegate relationship

///////////////////////////////

Set Button Events

$_ (". Btn-big-test"). On (' click ', Function (e) {alert ($_ (this). attr ("title"));

$_ ("#removetest"). On ("click", Function () {$_ (". Btn-big-test"). Detach (' click '); alert ("event removed");});

$_ ("#addtest"). On ("click", Function () {//) destroys events in successive invocation mode and delegates new events to avoid multiple calls to $_ (". Btn-big-test"). Detach (' click '). On (' Click ', function (e) {alert (this.classname);}); Alert ("Event has been added"); });




JS implementation like jquery basic function Simple selector/Event/attribute

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.