开发者

Put all images in DIV tags

Theres a way using javascript function to automatically put div tags in all images?

Example:

<div class="example"><img src="..."><开发者_开发百科/div>


I know you didn’t ask for a jQuery-specific solution, but if using jQuery is an option, there’s a pretty easy answer — you can use .wrap():

$('img').wrap('<div class="example" />');

This will wrap all images in a div with class="example".

Of course, this is possible in plain JavaScript as well; loop through all img elements, and for every node, clone it, create a new div, append the clone to it, insert the div into the DOM before or after the original img element, and finally remove the original img element from the DOM.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜