我的内容包含许多TextNodes,段落和图片。我想将所有图片标记放在父div中。好吧,我认为没有直接的方法来做到这一点(在jQuery中,我的意思是)。我应该如何处理元素的这种位移。以下示例说明了我的意图:
原创内容:
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "PostFileRest")]
string PostFileRest(Stream fileContents);
}
处理后:
<div id='content'>
Here are some texts.
A second line.
<a href='http://google.com'>Click Here!</a>
Another Line.
<img src='image.jpg' />
Another another line.
<img src='secondImage.jpg' />
Last line and many more to come.
</div>
唯一不同的是,现在新的div正在包装每个图像。
答案 0 :(得分:3)
使用.wrap()
围绕每个元素包装HTML结构。
$("#content > img").wrap("<div class='image-parent'></div>");
&#13;
.image-parent {
border: 1px solid red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='content'>
Here are some texts.
A second line.
<a href='http://google.com'>Click Here!</a>
Another Line.
<img src='image.jpg' />
Another another line.
<img src='secondImage.jpg' />
Last line and many more to come.
</div>
&#13;