使用jQuery仅创建一个开头的div标签 [英] Create only an opening div tag using jquery

查看:68
本文介绍了使用jQuery仅创建一个开头的div标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我已经搜索了很多此站点,但似乎找不到答案,因此我将以更好的理由提出同样的问题.我正在尝试使用jquery添加一个开头的div标签.当我尝试使用之后,我最终得到.我了解到,jQuery不喜欢像我想要的那样本地创建标签.但是,情况如下:

Okay, so I have searched this site quite a bit and I can't seem to find the answer, so I am going to ask the same question with a better reason. I am trying to use jquery to add an opening div tag. When I try to use the after I end up with . I understand that jquery does not like to natively create tags like I want. However, here is the situation:

我有一个动态表单生成器,用于显示表单.我正在表单中使用多个标题作为节标题.我想使用jquery手风琴,它希望看到div元素包装了手风琴框架的内容,并且希望在my之后直接看到它.问题在于,开始和结束div标签之间的元素数量是动态的.所以我不能只包装一些东西并完成它.我想做的是这样的:

I have a dynamic form generator that I am using to display a form. There are several headers I am using as section headers from within the form. I want to use the jquery accordion, and it wants to see a div element wrap the contents of the accordian frame, and it wants to see it directly after my . The problem is that the number of elements in between the opening and closing div tag is dynamic. So I can't just wrap something and be done with it. What I want to do is something like this:

<h3>Section 1</h3>

...此处的动态代码部分

...Dynamic code section here

<h3>Section 2</h3>

我想用jquery结束这样的事情:

I want to use jquery to end up with something like this:

<h3>Section 1</h3>
<div>

此处是动态代码部分

</div>
<h3>Section 2</h3>
<div>

更多动态代码

</div>

我知道我可以找到我所知道的元素(h3标签)并插入html.问题是jquery不会让我.显然,我对我的信任不足以自行解决问题.任何人都对如何使其工作有任何想法?

I know that I can find elements that I know of (the h3 tags) and insert the html. The problem is that jquery won't let me. Apparently it doesn't trust me enough to balance things out on my own. Anyone have any ideas on how to get this to work??

推荐答案

使用 .wrapAll()

$('h3').each(function(){
   $(this).nextUntil('h3').wrapAll(document.createElement('div'));
});

演示: http://jsfiddle.net/ph8D2/

这篇关于使用jQuery仅创建一个开头的div标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆