jquery插入一个html块,以便它在目标之前开始并在目标之后结束 - 这可能吗?
我知道您可以使用 before()
、insertBefore()
、after()
和 在目标之前和之后添加插入 html 块>insertAfter()
。
我尝试做类似的事情
<script type="text/javascript">
$(document).ready(function(){
$("p").before("<div class='myContainer'>");
$("p").after("</div>");
});
</script>
<p>paragraph</p>
但是注入似乎会自动为我创建结束标签,有办法解决这个问题吗?
干杯
I understand you can add insert blocks of html before and after a target using before()
, insertBefore()
, after()
and insertAfter()
.
I tried to do something similar to this
<script type="text/javascript">
$(document).ready(function(){
$("p").before("<div class='myContainer'>");
$("p").after("</div>");
});
</script>
<p>paragraph</p>
But the inject seems to automatically create closing tags for me, is there a way around this?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个函数可以实现这一点:) 使用
.wrap()
,例如this:这将包装每个独立的
,您可以在此处查看演示,它改变了这个:
变成了这个:
There's a function for that :) Use
.wrap()
, like this:This will wrap each
<p>
independent, you can see a demo here, it changes this:Into this:
只是提一下,您也可以这样做:
然后稍后再将内容附加到 myDiv 中
Just to mention you could also do:
and then again at a later time append stuff to myDiv with