Jquery - How to change the parent of an element from H1 to P?
I have <h1>heading</h1> , how can I change it to <p>heading</p>
<h1>heading</h1>
<p>heading</p>
I think that could be $.unwrap , then $.wrap , but is there a better way?
$.unwrap
$.wrap
$('h1').wrapInner('<p/>').children().unwrap();
This question is pretty close (I would consider a duplicate of it) How to change an element (e.g. h1 - gt; h2) using jQuery / plain old javascript?
Using this solution, your answer will be similar to
var p = $('h4'); var a = $('<p/>'). append(p.contents()); p.replaceWith(a);
Test it here: http://jsbin.com/abaja/edit
I would create a new element with the contents of H1, then hide H1.
var contents = $('h1').html(); $('h1').after('<p>'+content+'<p>'); $('h1').hide();
HTML:
<h1 id="a">heading</h1>
JQuery
var $a = $('#a'); var contents = $a.contents(); $a.remove(); $('body').append('<p>' + contents + '</p>');
$.unwrap will remove the parent of the matching element. In other words, if #a was a child of a div , the div would be deleted, not h1 .
#a
div
h1
Source: https://habr.com/ru/post/1307828/More articles:How to check that javascript does not open the same window with the window.open function - javascriptSOAP SOAP_COMPRESSION_GZIP verfication, if it works - soapXPath and special characters - xpathstd :: basic_string full specialization (g ++ conflict) - c ++How to draw a CGImage with some alpha transparency? - iphoneSet Position UIBarButtonItem Programmatically - iphoneEclipse Word / Line Counting Tool - eclipseCreating an Android application from ant via Hudson - chicken and egg problem - androidHow to change an element (e.g. h1 -> h2) using jQuery / plain old javascript? - javascriptUsing '.' in your key name in MongoDB (PyMongo) - mongodbAll Articles