开发者

Put all images in DIV tags

开发者 https://www.devze.com 2023-02-05 10:27 出处:网络
Theres a way using javascript function to automatically put div tags in all images? Example: <div class=\"example\"><img src=\"...\"><开发者_开发百科/div>

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

精彩评论

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