Aquí es un método que utilizo para reemplazar las etiquetas HTML en jQuery:
// Iterate over each element and replace the tag while maintaining attributes
$('span#anId').each(function() {
// Create a new element and assign it attributes from the current element
var NewElement = $("<a />");
$.each(this.attributes, function(i, attrib){
$(NewElement).attr(attrib.name, attrib.value);
});
// Replace the current element with the new one and carry over the contents
$(this).replaceWith(function() {
return $(NewElement).append($(this).contents());
});
});
La función each
utilizo en la primera línea es algo innecesario en este caso, como estamos seleccionando un solo elemento por id . Todavía prefiero usar each
aquí, ya que permitiría que este mismo código recorra todos los elementos con una clase específica también.
http://stackoverflow.com/questions/918792/use-jquery-to-change-an -html-tag – Babiker
parece que no tiene atributos sobre – Ben