Created
September 3, 2018 15:02
-
-
Save blubbll/53ae73eb04bfdf54e9f899a360317e09 to your computer and use it in GitHub Desktop.
jQuery replaceWith
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
//HTML-Tag mit jQuery ändern | |
$.extend({ | |
replaceTag: function (element, tagName, withDataAndEvents, deepWithDataAndEvents) { | |
var newTag = $("<" + tagName + ">")[0]; | |
$.each(element.attributes, function () { | |
newTag.setAttribute(this.name, this.value); | |
}); | |
$(element).contents().clone(withDataAndEvents, deepWithDataAndEvents).appendTo(newTag); | |
return newTag; | |
} | |
}) | |
$.fn.extend({ | |
replaceTag: function (tagName, withDataAndEvents, deepWithDataAndEvents) { | |
// Use map to reconstruct the selector with newly created elements | |
return this.map(function () { | |
return jQuery.replaceTag(this, tagName, withDataAndEvents, deepWithDataAndEvents); | |
}) | |
} | |
}) | |
$(this).replaceWith($(this).clone().replaceTag("a").attr('href', $(this).data('href'))); //Element zu link machen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment