开发者

Wrap content in DIV

开发者 https://www.devze.com 2022-12-08 04:35 出处:网络
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>

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");
0

精彩评论

暂无评论...
验证码 换一张
取 消