.append(),prePEND()。经过()和。之前() [英] .append(), prepend(), .after() and .before()

查看:330
本文介绍了.append(),prePEND()。经过()和。之前()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty精通编码,但现在,然后我遇到code似乎基本上做同样的事情。我的主要问题在这里,为什么你会使用 .append()而不是。经过()或副诗句?

我一直在寻找,似乎无法找到这两个和何时使用它们,何时不。

之间的差异明确定义

什么是一个比其他的好处,还我为什么会用一个而不是其他?有人可以解释这样对我?

  VAR的txt = $('#'+ ID +'跨度:第一')HTML();
$('#'+ ID +'a.append')。生活('点击',功能(E){
    亦即preventDefault();
    $('#'+ ID +'.innerDiv')追加(TXT)。
});
$('#'+ ID +'一个。prePEND')。生活('点击',功能(E){
    亦即preventDefault();
    $('#'+ ID +'.innerDiv')prePEND(TXT);
});
$('#'+ ID +'a.after')。生活('点击',功能(E){
    亦即preventDefault();
    $('#'+ ID +'.innerDiv')后(TXT);
});
$('#'+ ID +'a.before')。生活('点击',功能(E){
    亦即preventDefault();
    $('#'+ ID +'.innerDiv')前(TXT)。
});


解决方案

见:


.append()最后指数键,结果把一个元素内的数据
。prePEND()放prepending ELEM在第一指数


假设:

 < D​​IV CLASS ='A'> //< ---你想格C在此追加
  < D​​IV CLASS ='B'> B< / DIV>
< / DIV>

.append()执行它看起来是这样的:

  $('A')追加($('C'));

执行后:

 < D​​IV CLASS ='A'> //< ---你想格C在此追加
  < D​​IV CLASS ='B'> B< / DIV>
  < D​​IV CLASS ='C'> C< / DIV>
< / DIV>


拨弄在执行.append()。


prePEND()执行它看起来就像这样:

  $('A')prePEND($('C'));

执行后:

 < D​​IV CLASS ='A'> //< ---你想格C在此追加
  < D​​IV CLASS ='C'> C< / DIV>
  < D​​IV CLASS ='B'> B< / DIV>
< / DIV>


拨弄。prePEND()的执行。


。经过()把该元素的元素结果后
。之前()把该元素的元素之前


使用后:

  $('A')后($('C'));

执行后:

 < D​​IV CLASS ='A'>
  < D​​IV CLASS ='B'> B< / DIV>
< / DIV>
< D​​IV CLASS ='C'> C< / DIV> //其中p ----这将放在这里


拨弄在执行中。经过()。


使用前:

  $('A')前($('C'));

执行后:

 < D​​IV CLASS ='C'> C< / DIV> //其中p ----这将放在这里
< D​​IV CLASS ='A'>
  < D​​IV CLASS ='B'> B< / DIV>
< / DIV>


拨弄在执行中。之前()。


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?

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
.prepend() puts the prepending elem at 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:

$('.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.


when .prepend() executes it will look like this:

$('.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.


.after() puts the element after the element
.before() puts the element before the element


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.


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.


这篇关于.append(),prePEND()。经过()和。之前()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆