简体   繁体   English

jQuery-如何在HTML中移动结束标记

[英]jQuery - How do I Move a closing tag in my HTML

I am looking for a way to move just a closing form tag using jQuery. 我正在寻找一种使用jQuery移动结束表单标记的方法。

Currently the HTML looks something like this: 目前,HTML看起来像这样:

<form class="myform">
  <input class="text_field" id="profile_nickname" name="profile[nickname]" size="30" type="text" value="" data-validate="true">
</form>
<div class="form-action">
  <submit class="x">
</div>

Because the submit tag is outside of the form, the submit button doesn't work. 由于submit标签不在表单中,因此submit按钮不起作用。

I want to say something like: 我想说些类似的话:

$(".myform").remove('</form>')
$('</form>').appendTo($('.form-action'))

This of course does not work. 这当然是行不通的。 Any Ideas would be appreciated. 任何想法,将不胜感激。

.append()不能像字符串串联那样工作,您可以在其中切成一片并将其粘贴到其他地方...您需要移动dom元素

$('.form-action').appendTo('.myform')

you can use click event to sent out the form 您可以使用click事件发送表格

$(".form-action").click(submitForm);

function submitForm(){
$(".myform").submit();
}

you can clone the old object,append the new one to "form",and hide the old one. 您可以克隆旧对象,将新对象附加到“窗体”,然后隐藏旧对象。

var $new = $('.form-action').clone();
$('.form-action').hide();
$new.show();
$new.appendTo('.myform');

This feels really wrong, but I know what its like to work under bad conditions with a hateful content management system... So here it goes... 这确实是错误的,但是我知道在恶劣的条件下使用可恶的内容管理系统会产生什么样的后果。

If there is really no other way around it you can reset the HTML content with . 如果确实没有其他解决方法,则可以使用重置HTML内容。 html() like so: html()像这样:

Working Example 工作实例

$('body').html(
    '<form class="myform">' +
    '<input class="text_field" id="profile_nickname" name="profile[nickname]" size="30" type="text" value="" data-validate="true">' +
    '<div class="form-action">' +
    ' <submit class="x">' +
    '</div></form>');

This is an ugly way to do it, but if you're backed into a corner it will work. 这是一个丑陋的方法,但是如果您陷入困境,它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM