For example I have this piece of code:
<div class="myclass">Hello everybody!</div>
Then I want to wrap a <p>
tag around the text to have this:
<div class="myclass"><p>Hello everybody!</p></div>
How can I do this with jQuery?
I tried this:
$('.myclass').text().wrap('p');
But didn't work.
try something like this
$(function(){
$('.myclass').wrapInner('<p></p>');
})
REFERENCE :
http://api.jquery.com/wrapInner/
Alternative
$(function(){
var txt = '<p>' + $('.myclass').text() + '</p>';
$('.myclass').html(txt);
})
Again to remove p tag
$(function(){
var txt = $('.myclass').text();
$('.myclass').html(txt);
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.