jQuery操作(常用方法)-插入和刪除

來源:互聯網
上載者:User

jQuery操作(常用方法)-插入和刪除

文檔處理(刪除)
remove([expr])/元素刪除/

 function jq1(){            $(function(){                 $('div').remove('#odiv1'); /*從DOM中刪除所有匹配的元素。    */            })        }        jq1();

html片段


div
div_id = odiv1
span 
h1

文檔處理(插入)

(function(){                 /*把所有匹配的元素插入到另一個、指定的元素元素集合的前面。*/                   $('.odiv').insertBefore('span'); }()) ;(function(){  /*把所有匹配的元素插入到另一個、指定的元素元素集合的後面。  */                  $('.odiv').insertAfter('span');  }());(function(){/*把所有匹配的元素追加到另一個指定的元素元素集合中。   */                  $('span').appendTo('.odiv');  }()); (function(){ /*把所有匹配的元素前置到另一個、指定的元素元素集合中(和appendTo()相反把指定元素添加到匹配元素之中)*/                 $('span').prependTo('.odiv');   }());

接下來解釋

insertBefore與before

insertAfter與after

appendTo與append

prependTo與prepend

==>>區別:後續操作變了

如:

$('.odiv').insertBefore('span').css('background','red');$('.odiv').before('span').css('background','red');

兩者效果一致但是呢insertBefore(‘必須是content’),而before(content|fn)中content和函數fn是可選的即可以使用函數來新增內容或者傳遞參數

文法:$(selector).append(function(index,html));
如:

$('span').before($(#odiv)[0]);

/*以上所排什麼insertAfter與after啊,appendTo與append啊,prependTo與prepend都是這個道理

*/
HTML代碼塊


span 
div

(大神勿噴啊!)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.