jQuery แทนที่ด้วย ()เมธอด

❮ วิธีการ jQuery HTML/CSS

ตัวอย่าง

แทนที่ <p> องค์ประกอบแรกด้วยข้อความใหม่:

$("button").click(function(){
   $("p:first").replaceWith("Hello world!");
});

ความหมายและการใช้งาน

ReplaceWith() วิธีการแทนที่องค์ประกอบที่เลือกด้วยเนื้อหาใหม่


ไวยากรณ์

$(selector).replaceWith(content,function(index))

Parameter Description
content Required. Specifies the content to insert (can contain HTML tags)

Possible values:

  • HTML elements
  • jQuery objects
  • DOM elements
function(index) Optional. Specifies a function that returns the content to replace
  • index - Returns the index position of the element in the set

ลองด้วยตัวคุณเอง - ตัวอย่าง


วิธีใช้ฟังก์ชันเพื่อแทนที่องค์ประกอบที่เลือกด้วยเนื้อหาใหม่


❮ วิธีการ jQuery HTML/CSS