欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery?追加元素append、prepend、before、after用法與區(qū)別分析

 更新時間:2023年05月26日 23:23:53   投稿:mdxy-dxy  
在jquery中append、prepend、before、after方法是對數據元素節(jié)點的操作系列方法了,這些方法大家了解嗎?如果不了解就可以和小編來看看它們用法

功能說明

對比的看

before和prepend

before:在被選元素之前添加的內容(內容外面)

prepend:在被選元素的前面添加的內容(內容的里面)

同理

after和append

after:在被選元素之后添加的內容(內容外面)

append:在被選元素的后面添加的內容(內容的里面)

jquery的插入外部

after() 元素外的后面插入
insertAfter() 把內容插入到元素外的后面
before() 在元素外的前面插入內容
insertBefore() 把內容插入到目標元素外的后面

詳細說明

append與prepedn都是元素里面操作,這樣就比較簡單了

一、after()和before()方法的區(qū)別

    after()——其方法是將方法里面的參數添加到jquery對象后面去;
    如:A.after(B)的意思是將B放到A后面去;
    before()——其方法是將方法里面的參數添加到jquery對象前面去。
    如:A.before(B)的意思是將A放到B前面去;  

二、insertAfter()和insertBefore()的方法的區(qū)別

    其實是將元素對調位置;
    可以是頁面上已有元素;也可以是動態(tài)添加進來的元素。
    如:A.insertAfter(B);即將A元素調換到B元素后面;
    如<span>CC</span><p>HELLO</p>使用$("span").insertAfter($("p"))后,就變?yōu)?lt;p>HELLO</p><span>CC</span>了。兩者位置調換了 

三、append()和appendTo()方法的區(qū)別

    append()——其方法是將方法里面的參數添加到jquery對象中來;
    如:A.append(B)的意思是將B放到A中來,后面追加,A的子元素的最后一個位置;
    appendTo()——其方法是將jquery對象添加到appendTo指定的參數中去。
    如:A.appendTo(B)的意思是將A放到B中去,后面追加,B的子元素的最后一個位置; 

四、prepend()和prependTo()方法的區(qū)別

    append()——其方法是將方法里面的參數添加到jquery對象中來;
    如:A.append(B)的意思是將B放到A中來,插入到A的子元素的第一個位置;
    appendTo()——其方法是將jquery對象添加到appendTo指定的參數中去。
    如:A.appendTo(B)的意思是將A放到B中去,插入到B的子元素的第一個位置; 

例子

1、insert局部方法

/**
 * 在父級元素上操作DOM
 * @param {Object} parent  父級元素,可以是element,也可以是Yquery對象
 * @param {String} position 位置: beforebegin/afterbegin/beforeend/afterend
 * @param {*}   any   任何:string/text/object
 * @param {Number} index  序號,如果大于0則復制節(jié)點
 * @return {Undefined}
 * @version 1.0
 * 2013年12月2日17:08:26
 */
function _insert(parent, position, any, index) {
  if ($.isFunction(any)) {
    any = any.call(parent);
  }
  // 字符串
  if ($.isString(any)) {
    if (regTag.test(any)) {
      parent.insertAdjacentHTML(position, any);
    } else {
      parent.insertAdjacentText(position, any);
    }
  }
  // 數字
  else if ($.isNumber(any)) {
    parent.insertAdjacentText(position, any);
  }
  // 元素
  else if ($.isElement(any)) {
    parent.insertAdjacentElement(position, index > 0 ? any.cloneNode(!0) : any);
  }
  // Yquery
  else if (_isYquery(any)) {
    any.each(function() {
      _insert(parent, position, this);
    });
  }
}

2、append、prepend、before、after

$.fn = {
  /**
   * 追插
   * 將元素后插到當前元素(集合)內
   * @param {String/Element/Function} any
   * @return this
   * @version 1.0
   * 2013年12月29日1:44:15
   */
  append: function(any) {
    return this.each(function(index) {
      _insert(this, 'beforeend', any, index);
    });
  },
  /**
   * 補插
   * 將元素前插到當前元素(集合)內
   * @param {String/Element/Function} any
   * @return this
   * @version 1.0
   * 2013年12月29日1:44:15
   */
  prepend: function(any) {
    return this.each(function(index) {
      _insert(this, 'afterbegin', any, index);
    });
  },
  /**
   * 前插
   * 將元素前插到當前元素(集合)前
   * @param {String/Element/Function} any
   * @return this
   * @version 1.0
   * 2013年12月29日1:44:15
   */
  before: function(any) {
    return this.each(function(index) {
      _insert(this, 'beforebegin', any, index);
    });
  },
  /**
   * 后插
   * 將元素后插到當前元素(集合)后
   * @param {String/Element/Function} any
   * @return this
   * @version 1.0
   * 2013年12月29日1:44:15
   */
  after: function(any) {
    return this.each(function(index) {
      _insert(this, 'afterend', any, index);
    });
  }
};

3、prependTo、prependTo、insertBefore、insertAfter

這些帶后綴的與上面的不同的是,返回的結果不一樣。如:

$('#demo').append('<a/>');
// => 返回的是 $('#demo')
$('<a/>').appendTo($('#demo'));
// => 返回的是$('a');

因此兩者的關系只是返回結果不一樣,其他的都一樣,可以這么解決:

_each({
  appendTo: 'append',
  prependTo: 'prepend',
  insertBefore: 'before',
  insertAfter: 'after'
}, function(key, val) {
  $.fn[key] = function(selector) {
    this.each(function() {
      $(selector)[val](this);
    });
    return this;
  };
});

jquery 追加元素的方法(append prepend after before 的區(qū)別)

append() 方法在被選元素的結尾插入內容。

prepend() 方法在被選元素的開頭插入內容。

after() 方法在被選元素之后插入內容。

before() 方法在被選元素之前插入內容。

<script type="text/javascript" src="http://common.jb51.net/jslib/jquery/jquery.min.js"></script>
<div class="testdiv">
  <ul>
    <li>第一個li標簽</li>
  </ul>
</div>

<script>

  //append
  $('.testdiv ul').append('<li>append 插入的li</li>');
  //prepend
  $('.testdiv ul').prepend('<li>prepend 插入的li</li>');
  //after
  $('.testdiv ul').after('<li>after 插入的li</li>');
  //before
  $('.testdiv ul').before('<li>before 插入的li</li>');

</script>

效果圖

html結構圖

相關文章

最新評論