การเปลี่ยนข้อความภายในแท็ก <strong>
ใช้ jQuery เลือก element ด้วย ID แล้วเปลี่ยนเนื้อหาด้วยเมธอด .html()
ตัวอย่างโค้ดเต็ม
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<strong id="strongDemo">Replace This strong tag</strong>
<script>
$(document).ready(function(){
$("#strongDemo").html("Actual value of 5+10 is 15.....");
});
</script>
</body>
</html>
วิธีรัน
บันทึกเป็น index.html แล้วเปิดด้วย Live Server ใน VS Code
ผลลัพธ์
ข้อความภายใน <strong> จะเปลี่ยนเป็น "Actual value of 5+10 is 15....."
คำอธิบายเพิ่มเติม
- ใช้
$(document).ready()ให้โค้ดรันหลังโหลด DOM เสร็จ $("#strongDemo")เลือก element ที่มี id="strongDemo".html("...")ตั้งค่า HTML เนื้อหาใหม่ (รองรับแท็ก HTML ได้)- หากต้องการแค่ข้อความธรรมดา ใช้
.text("...")แทน