Created
January 22, 2017 09:49
-
-
Save Kcko/8c489234982cd325f6ede449408a0f69 to your computer and use it in GitHub Desktop.
jQuery - own methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(function(){ | |
| jQuery.extend(jQuery.expr[':'], { | |
| 'child-of' : function(a,b,c) { | |
| return (jQuery(a).parents(c[3]).length > 0); | |
| } | |
| }); | |
| //'child-of' is now a valid selector: | |
| $("li:child-of(ul.test)").css("background","#000"); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(function(){ | |
| jQuery.fn.isChildOf = function(b){return (this.parents(b).length > 0);}; | |
| //Now we can evaluate like this: | |
| if ( $("li").isChildOf("ul") ){ | |
| //Obviously, the li is a child of the ul so this code is executed | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(function(){ | |
| jQuery.fn.putBefore = function(dest){ | |
| return this.each(function(){ | |
| $(dest).before($(this)); | |
| }); | |
| } | |
| jQuery.fn.putAfter = function(dest){ | |
| return this.each(function(){ | |
| $(dest).after($(this)); | |
| }); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment