Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

คุณสมบัติ HTML DOM del dateTime


คุณสมบัติ dateTime ของ HTML DOM del เกี่ยวข้องกับองค์ประกอบ HTML <del> ใช้บอกผู้ใช้ว่าเมื่อไหร่ข้อความบนเว็บไซต์ถูกลบ บอกวันที่และเวลาที่ข้อความถูกลบ

ไวยากรณ์ (Syntax)

delObject.dateTime = YYYY-MM-DDThh:mm:ssTZD

ที่นี่ YYYY=ปี MM=เดือน DD=วัน T=ตัวคั่นหรือช่องว่าง hh=ชั่วโมง mm=นาที ss=วินาที TZD=ตัวกำหนดเขตเวลา

ตัวอย่าง (Example)

<!DOCTYPE html> <html> <head> 
<title>del dateTime property example</title> 
<style> #Sample{color:blue}; </style> 
</head> 
<body> 
<h2>del dateTime property example</h2> 
<p><del id="Del1" datetime="2019-02-22T05:10:22Z">Some text has been deleted</del></p> 
<p>Click the below button to change the datetime attribute value of the deleted text above</p> 
<button onclick="changeDate()">CHANGE</button> 
<p id="Sample"></p> 
<script> 
  function changeDate() { 
    document.getElementById("Del1").dateTime = "2019-06-27T10:20:02Z"; 
    document.getElementById("Sample").innerHTML = "The datetime attribute attribute was changed to '2019-06-27T10:20:02Z'."; 
  } 
</script> </body> </html>

ผลลัพธ์ (Output)

ผลลัพธ์ที่ได้:

คุณสมบัติ HTML DOM del dateTime

เมื่อคลิกปุ่ม CHANGE:

คุณสมบัติ HTML DOM del dateTime

คำอธิบายโค้ด

สร้าง <del> element ใน <p> id "Del1" datetime="2019-02-22T05:10:22Z":

<p><del id="Del1" datetime="2019-02-22T05:10:22Z">Some text has been deleted</del></p>

สร้างปุ่ม CHANGE ทำงาน changeDate() เมื่อคลิก:

<button onclick="changeDate()">CHANGE</button>

changeDate() ดึง <del> element ตั้ง dateTime = "2019-06-27T10:20:02Z" แสดงการเปลี่ยนแปลงในพารากราฟ id "Sample" ด้วย innerHTML ข้อความใน "Sample" สีน้ำเงินเพราะมี style ตาม id:

function changeDate() { 
  document.getElementById("Del1").dateTime = "2019-06-27T10:20:02Z"; 
  document.getElementById("Sample").innerHTML = "The datetime attribute attribute was changed to '2019-06-27T10:20:02Z'."; 
}