JQuery是否有“移动”?功能?或者有更紧凑的方式吗? [英] Does JQuery have a "move" function? Or is there a more compact way of doing this?
问题描述
当将JQuery与内容管理系统结合使用时,我发现自己经常会编写代码片段,例如
When using JQuery in conjunction with a content management system, I find myself often writing snippets of code such as
$(document).ready(function()
{
var thishtml = $('#some-element')[0].outerHTML;
$('#some-element').remove();
$('#hmpg-btm-box-holder').prepend(thishtml);
});
JQuery是否具有以更紧凑的方式实现这一目标的功能?
Does JQuery have a functionality to accomplish that in a more compact fashion?
推荐答案
这应该足够了:
$(document).ready(function()
{
$('#hmpg-btm-box-holder').prepend($('#some-element'));
});
我尽量避免使用 outerHTML
(并且最好还是 innerHTML
,除非您例如通过AJAX从可信服务器接收一些HTML并希望将其包含在文档中)。
I'd avoid using outerHTML
whenever possible (and preferably innerHTML
also, unless you e.g. receive some HTML from a trusted server via AJAX and want to include it in the document).
将元素从元素
转换为字符串(HTML)表示并返回元素
很容易造成不必要的影响,例如删除附加到它的所有事件处理程序。
Converting an element from an Element
to a string (HTML) representation and back into an Element
can easily cause unwanted effects like removing all event handlers attached to it.
这篇关于JQuery是否有“移动”?功能?或者有更紧凑的方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!