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.
精彩评论