简体   繁体   English

.append(), prepend(), .after() 和 .before()

[英].append(), prepend(), .after() and .before()

I am pretty proficient with coding, but now and then I come across code that seems to do basically the same thing.我非常精通编码,但有时我会遇到似乎做基本相同事情的代码。 My main question here is, why would you use .append() rather then .after() or vice verses?我的主要问题是,为什么要使用.append()而不是.after()或反之亦然?

I have been looking and cannot seem to find a clear definition of the differences between the two and when to use them and when not to.我一直在寻找并且似乎无法找到两者之间差异的明确定义以及何时使用它们以及何时不使用它们。

What are the benefits of one over the other and also why would i use one rather then the other??一个比另一个有什么好处,为什么我要使用一个而不是另一个? Can someone please explain this to me?有人可以向我解释一下吗?

var txt = $('#' + id + ' span:first').html();
$('#' + id + ' a.append').live('click', function (e) {
    e.preventDefault();
    $('#' + id + ' .innerDiv').append(txt);
});
$('#' + id + ' a.prepend').live('click', function (e) {
    e.preventDefault();
    $('#' + id + ' .innerDiv').prepend(txt);
});
$('#' + id + ' a.after').live('click', function (e) {
    e.preventDefault();
    $('#' + id + ' .innerDiv').after(txt);
});
$('#' + id + ' a.before').live('click', function (e) {
    e.preventDefault();
    $('#' + id + ' .innerDiv').before(txt);
});

See:看:


.append() puts data inside an element at last index and .append()将数据放入元素中的last index
.prepend() puts the prepending elem at first index .prepend()将前置元素放在first index


suppose:认为:

<div class='a'> //<---you want div c to append in this
  <div class='b'>b</div>
</div>

when .append() executes it will look like this:.append()执行时,它将如下所示:

$('.a').append($('.c'));

after execution:执行后:

<div class='a'> //<---you want div c to append in this
  <div class='b'>b</div>
  <div class='c'>c</div>
</div>

Fiddle with .append() in execution.在执行中摆弄 .append() 。


when .prepend() executes it will look like this:.prepend()执行时,它将如下所示:

$('.a').prepend($('.c'));

after execution:执行后:

<div class='a'> //<---you want div c to append in this
  <div class='c'>c</div>
  <div class='b'>b</div>
</div>

Fiddle with .prepend() in execution.在执行中摆弄 .prepend() 。


.after() puts the element after the element .after()将元素放在元素之后
.before() puts the element before the element .before()将元素放在元素之前


using after:使用后:

$('.a').after($('.c'));

after execution:执行后:

<div class='a'>
  <div class='b'>b</div>
</div>
<div class='c'>c</div> //<----this will be placed here

Fiddle with .after() in execution.在执行中摆弄 .after() 。


using before:之前使用:

$('.a').before($('.c'));

after execution:执行后:

<div class='c'>c</div> //<----this will be placed here
<div class='a'>
  <div class='b'>b</div>
</div>

Fiddle with .before() in execution.在执行中摆弄 .before() 。


This image displayed below gives a clear understanding and shows the exact difference between .append() , .prepend() , .after() and .before()这个图像显示下面给出了一个清楚的了解和显示之间的精确差值.append() .prepend() .after().before()

jQuery 信息图

You can see from the image that .append() and .prepend() adds the new elements as child elements (brown colored) to the target.您可以从图像中看到.append().prepend()将新元素作为元素(棕色)添加到目标中。

And .after() and .before() adds the new elements as sibling elements (black colored) to the target. .before() .after().before()将新元素作为兄弟元素(黑色)添加到目标中。

Here is a DEMO for better understanding.这是一个DEMO,以便更好地理解。


EDIT: the flipped versions of those functions:编辑:这些函数的翻转版本:

jQuery 插入信息图,以及函数的翻转版本

Using this code :使用此代码

var $target = $('.target');

$target.append('<div class="child">1. append</div>');
$target.prepend('<div class="child">2. prepend</div>');
$target.before('<div class="sibling">3. before</div>');
$target.after('<div class="sibling">4. after</div>');

$('<div class="child flipped">or appendTo</div>').appendTo($target);
$('<div class="child flipped">or prependTo</div>').prependTo($target);
$('<div class="sibling flipped">or insertBefore</div>').insertBefore($target);
$('<div class="sibling flipped">or insertAfter</div>').insertAfter($target);

on this target:在这个目标上:

<div class="target">
    This is the target div to which new elements are associated using jQuery
</div>

So although these functions flip the parameter order, each creates the same element nesting:因此,尽管这些函数翻转了参数顺序,但每个函数都会创建相同的元素嵌套:

var $div = $('<div>').append($('<img>'));
var $img = $('<img>').appendTo($('<div>'))

...but they return a different element. ...但它们返回不同的元素。 This matters for method chaining .这对于方法链很重要

append()prepend()用于在元素内部插入内容(使内容成为其子元素),而after()before()在元素外部插入内容(使内容成为其兄弟)。

The best way is going to documentation.最好的方法是去文档。

.append() vs .after() .append().after()

  • . . append() : Insert content, specified by the parameter, to the end of each element in the set of matched elements. append() :将参数指定的内容插入匹配元素集中每个元素的末尾
  • . . after() : Insert content, specified by the parameter, after each element in the set of matched elements. after() :在匹配元素集中的每个元素之后插入由参数指定的内容。

.prepend() vs .before() .prepend() VS .before()

  • prepend() : Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. prepend() :将参数指定的内容插入匹配元素集中每个元素的开头
  • . . before() : Insert content, specified by the parameter, before each element in the set of matched elements. before() :在匹配元素集中的每个元素之前插入由参数指定的内容。

So, append and prepend refers to child of the object whereas after and before refers to sibling of the the object.因此, append 和 prepend 是指对象的子对象,而 after 和 before 是指对象的兄弟对象。

There is a basic difference between .append() and .after() and .prepend() and .before() .之间存在基本差异.append().after().prepend().before()

.append() adds the parameter element inside the selector element's tag at the very end whereas the .after() adds the parameter element after the element's tag . .append()在选择器元素标签的最后添加参数元素.after()在元素标签后添加参数元素。

The vice-versa is for .prepend() and .before() .所述反之亦然为.prepend().before()

Fiddle小提琴

There is no extra advantage for each of them.他们每个人都没有额外的优势。 It totally depends on your scenario.这完全取决于你的场景。 Code below shows their difference.下面的代码显示了它们的区别。

    Before inserts your html here
<div id="mainTabsDiv">
    Prepend inserts your html here
    <div id="homeTabDiv">
        <span>
            Home
        </span>
    </div>
    <div id="aboutUsTabDiv">
        <span>
            About Us
        </span>
    </div>
    <div id="contactUsTabDiv">
        <span>
            Contact Us
        </span>
    </div>
    Append inserts your html here
</div>
After inserts your html here
<div></div>    
// <-- $(".root").before("<div></div>");
<div class="root">
  // <-- $(".root").prepend("<div></div>");
  <div></div>
  // <-- $(".root").append("<div></div>");
</div>
// <-- $(".root").after("<div></div>");
<div></div>    

Imagine the DOM (HTML page) as a tree right.DOM (HTML 页面)想象成一棵树吧。 The HTML elements are the nodes of this tree. HTML 元素是这棵树的节点。

The append() adds a new node to the child of the node you called it on. append()将一个新节点添加到您调用它的节点的child节点。

Example:$("#mydiv").append("<p>Hello there</p>") 

creates a child node <p> to <div>

The after() adds a new node as a sibling or at the same level or child to the parent of the node you called it on. after()将一个新节点作为兄弟节点或在同一级别或子节点添加到您调用它的节点的父节点。

To try and answer your main question:尝试回答您的主要问题:

why would you use .append() rather then .after() or vice verses?为什么要使用 .append() 而不是 .after() 或反之亦然?

When you are manipulating the DOM with jquery the methods you use depend on the result you want and a frequent use is to replace content.当您使用 jquery 操作 DOM 时,您使用的方法取决于您想要的结果,并且经常使用的是替换内容。

In replacing content you want to .remove() the content and replace it with new content.在替换内容时,您希望.remove()内容并将其替换为新内容。 If you .remove() the existing tag and then try to use .append() it won't work because the tag itself has been removed, whereas if you use .after() , the new content is added 'outside' the (now removed) tag and isn't affected by the previous .remove() .如果您.remove()现有标签然后尝试使用.append()它将不起作用,因为标签本身已被删除,而如果您使用.after() ,则新内容会添加到 (现在已删除) 标记并且不受之前的.remove()

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

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