开发者

Wrap content in DIV

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜