jQuery ก่อน ()เมธอด

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

ตัวอย่าง

แทรกเนื้อหาก่อนแต่ละ <p> องค์ประกอบ:

$("button").click(function(){
  $("p").before("<p>Hello world!</p>");
});

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

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

เคล็ดลับ:   หากต้องการแทรกเนื้อหาหลังองค์ประกอบที่เลือก ให้ใช้ เมธอดafter()


ไวยากรณ์

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

Parameter Description
content 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 insert
  • index - Returns the index position of the element in the set

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


แทรกเนื้อหาด้วยเมธอด before()


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


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