我有:
<div>
<a> //this
<span>sometext<span> //problem
</a>
<a></a> //this
<a></a> //this
<span>sometext</span> //this
<div><div>
<input>
</div>
我需要包装标记的元素并得到如下结果:
<div>
<div> //wrapped
<a>
<span>sometext<span>
</a>
<a></a>
<a></a>
<span>sometext</span>
</div> //wrapped
<div><div>
<input>
</div>
但当我用$("div a, div span").wrapAll("<div></div>");
执行此操作时
它从span
开始a
,但我没有写div a span
。所以我明白了:
<div>
<div> //wrapped
<a>
</a>
<span>sometext<span> //need to be child of tag a
<a></a>
<a></a>
<span>sometext</span>
</div> //wrapped
<div><div>
<input>
</div>
答案 0 :(得分:0)
您应该按课程$(".classname").wrapAll("");
执行此操作,并将课程classname
添加到您的第一个div
答案 1 :(得分:-1)
但最好将一个类/其他标识符添加到容器div
<div class="someclass">
<a> //this
<span>sometext</span> //problem
</a>
<a></a> //this
<a></a> //this
<span>sometext</span> //this
<div></div>
<input />
</div>
然后
$("div.someclass").children("a, span").wrapAll("<div />");
演示:Fiddle