Created
March 25, 2014 10:46
-
-
Save bennadel/9759092 to your computer and use it in GitHub Desktop.
Ask Ben: Overriding Core jQuery Methods
This file contains 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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html> | |
<head> | |
<title>Overriding jQuery Methods</title> | |
<script type="text/javascript" src="jquery-1.3.2.js"></script> | |
<script type="text/javascript"> | |
// Create a closure so that we can define intermediary | |
// method pointers that don't collide with other items | |
// in the global name space. | |
(function(){ | |
// Store a reference to the original remove method. | |
var originalRemoveMethod = jQuery.fn.remove; | |
// Define overriding method. | |
jQuery.fn.remove = function(){ | |
// Log the fact that we are calling our override. | |
console.log( "Override method" ); | |
// Execute the original method. | |
originalRemoveMethod.apply( this, arguments ); | |
} | |
})(); | |
// When DOM is ready, initialize. | |
$(function(){ | |
$( "a" ) | |
.attr( "href", "javascript:void( 0 )" ) | |
.click( | |
function(){ | |
// Remove the target link. | |
$( this ).remove(); | |
// Cancel default event. | |
return( false ); | |
} | |
) | |
; | |
}); | |
</script> | |
</head> | |
<body> | |
<h1> | |
Override a jQuery Method | |
</h1> | |
<p> | |
<a>Remove Me 1</a> | |
<a>Remove Me 2</a> | |
<a>Remove Me 3</a> | |
</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment