[英]How to determine if there exists text at the beginning or end of a parent element? HTML DOM & JavaScript
[英]How to move the beginning and end tags of a HTML DOM element?
我怎么能改变这个:
<div class="container">
<div class="span-19">
<div id="content">
<!-- variable amount of content -->
<form class="move-me">
<!-- form -->
</form>
</div>
</div>
</div>
进入:
<form class="move-me">
<div class="container">
<div class="span-19">
<div id="content">
<!-- variable amount of content -->
<!-- form data -->
</div>
</div>
</div>
</form>
使用jQuery? 注意,我特别想要只移动<form>
开始和结束标记,而不是该表单的子元素。 我最好想要一些jQuery.fn
以便我可以这样做:
$('.move-me').changeNesting('.container');
提前致谢!
使用unwrap
和wrap
:
var toMove = $('.move-me');
toMove.children().unwrap();
$('.container').wrap(toMove);
更新 :请注意,如果表单具有嵌套的原始文本,则上述代码将不起作用。 您可以使用另一个标签来包装表单的子项以使其工作(也可以使用Yoshi在评论中指出的end
):
$('.container').wrap(
$('.move-me').wrapInner('<div class="nostyle"/>').children().unwrap().end()
);
使用.wrap()和.detach() ,就像这样:
$('.container').wrap( $('form.move-me').detach() );
$('.move-me').replaceWith(function() {
return $('*', this);
});
$('.container').wrap('<form class="move-me" />');
var $form=$('form.move-me')
$form.replaceWith($form.html()));
$('.container').wrap('<form class="move-me">');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.