问题
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