Can I use jQuery to surround an element with <form></form>?

南楼画角 提交于 2021-01-28 11:48:33

问题


I have the following HTML:

<div id="modal" ><div class="modal-window">
<ul class="action-tabs right">
<li><a href="#" title="Close window">
<img src="/Content/images/icons/fugue/cross-circle.png" width="16" height="16"></a></li>
</ul>
<div class="block-content"><h1>xx</h1>
<div class="modal-content>
...
</div>
</div>
</div>

I can't change the way this is coded but I need there to be a form element starting after the h1 heading and finishing after the end div of the class "modal-content". Note there's only one "block-content" and "modal-content".

Is there a way using jQuery that I could insert in a form tag as below:

<div id="modal" ><div class="modal-window">
<ul class="action-tabs right">
<li><a href="#" title="Close window">
<img src="/Content/images/icons/fugue/cross-circle.png" width="16" height="16"></a></li>
</ul>
<div class="block-content"><h1>xx</h1>
<form id="xx">
<div class="modal-content>
...
</div>
</form>
</div>
</div>

回答1:


Try this ..

$(".modal-content").wrap("<form id='xx'>")

Refer http://api.jquery.com/wrap/ for more options




回答2:


$('.modal-content').wrap('<form id="xx"></form>');



回答3:


Yes, use one of the wrap-functions: http://api.jquery.com/?s=wrap

$( '.modal-content' ).wrapAll( '<form />' );



回答4:


A simple example

$(".modal-content").wrap('<form id="xx"></form>');

See http://api.jquery.com/wrap/




回答5:


Yes, easilly:

$('.modal-content').wrap('<form id="xxx" />');

Read more on .wrap() here: http://api.jquery.com/wrap/



来源:https://stackoverflow.com/questions/12475321/can-i-use-jquery-to-surround-an-element-with-form-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!