simple question i think, but cant figure this out
How can I wrap particular text into DIV?
Have this
Price:<strong>£12.30 (Ex VAT)</strong>
And need to have this:
<div>Price:<开发者_Python百科;strong>£12.30 (Ex VAT)</strong></div>
Thanks for your help
You can use appendChild method :
var newDiv = document.createElement('div');
newDiv.innerHTML = "Price:<strong>£12.30 (Ex VAT)</strong>";
document.getElementById('parentDiv').appendChild(newDiv);
Here is the JQuery equvalent of the code above :
var newDiv = $("<div>Price:<strong>£12.30 (Ex VAT)</strong></div>");
$('#parentElement').append(newDiv);
Or :
$("<div>Price:<strong>£12.30 (Ex VAT)</strong></div>").appendTo("#parentElement");
var str = 'Price:<strong>£12.30 (Ex VAT)</strong>';
var div = $('<div />').html(str);
jQuery has a wrap function that does exactly what you are describing...
var str = "Price: <strong>£12.30 (Ex VAT)</strong>";
$(str).wrap("<div></div>").appendTo("body");
精彩评论