我对编码相当精通,但偶尔我会遇到一些代码,它们似乎基本上做着相同的事情.我的主要问题是,为什么你会使用.append()而不是.after()或副诗节?

我一直在寻找,似乎找不到两者之间的区别以及何时使用和何时不使用的明确定义.

一个比另一个有什么好处?为什么我要使用一个而不是另一个??谁能给我解释一下吗?

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);
});

推荐答案

请参见:


.append() puts data inside an element at last index and
.prepend() puts the prepending elem at first index


假设:

<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'));

执行后:

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

摆弄.执行中的append().


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

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

执行后:

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

在执行过程中摆弄.prepend().


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


之后使用:

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

执行后:

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

摆弄.在()之后执行.


在此之前使用:

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

执行后:

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

摆弄.在执行之前.


Javascript相关问答推荐

通过实现regex逻辑自定义数据表搜索

JQuery. show()工作,但. hide()不工作

PrivateRoute不是路由组件错误

用于编辑CSS样式的Java脚本

保持物品顺序的可变大小物品分配到平衡组的算法

删除加载页面时不存在的元素(JavaScript)

为列表中的项目设置动画

react -原生向量-图标笔划宽度

如何更改Html元素S的 colored颜色 ,然后将其褪色为原始 colored颜色

用另一个带有类名的div包装元素

有没有办法通过使用不同数组中的值进行排序

为什么NULL不能在构造函数的.Prototype中工作

我为什么要使用回调而不是等待?

我怎样才能点击一个元素,并获得一个与 puppeteer 师导航页面的URL?

有没有办法在R中创建一张具有多个色标的热图?

React数组查找不读取变量

使用Java脚本替换字符串中的小文本格式hashtag

如何调整下拉内容,使其不与其他元素重叠?

由于http.get,*ngIf的延迟很大

如何从图表中映射一组图表-js使用REACT