JQUERY添加、刪除元素

來源:互聯網
上載者:User

標籤:ges   size   rip   before   element   添加元素   文本   插入   document   

一、jQuery - 添加元素

1.append() - 在被選元素內部的結尾插入指定內容

2.prepend() - 在被選元素內部的開頭插入指定內容

3.after() - 在被選元素之後插入內容

4.before() - 在被選元素之前插入內容

添加單個元素

<script type="text/javascript" src="jquery-1.11.2.min.js"></script><body><p>111</p></body></html><script type="text/javascript">$(document).ready(function(e) { $("p").append("222"); $("p").prepend("333");  $("p").after("444");   $("p").before("555");});</script>

添加若干元素----重點                

<body><button onclick="tianjia()">追加文本</button></body></html><script type="text/javascript">function tianjia() {    var txt1="<p>文本1</p>";              // 使用 HTML 標籤建立文本    var txt2=$("<p></p>").text("文本2");  // 使用 jQuery 建立文本    var txt3=document.createElement("p");    txt3.innerHTML="文本3";               // 使用 DOM 建立文本 text with DOM    $("body").append(txt1,txt2,txt3);        // 追加新元素}</script>

二、jQuery - 刪除元素

1.remove() - 刪除被選元素(及其子項目)

2.empty() - 從被選元素中刪除子項目

<div id="div1">111111<p>2222222</p><p class="pp">3333333333</p><p class="pp">444444444</p></div><br><button>移除div元素</button></body></html><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    $("#div1").remove();         //刪除div及其裡邊的p所有內容         $("#div1").empty();         //刪除div裡面的p標籤裡面的內容           $("p").remove(".pp");      //刪除所有class為pp的p標籤  });});</script>

 

JQUERY添加、刪除元素

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.