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

คุณสมบัติ text ของ HTML DOM Title

คุณสมบัติ text ของ HTML DOM Title ใช้สำหรับ อ่านหรือกำหนด ค่าขององค์ประกอบ <title> ของเอกสาร

ไวยากรณ์ (Syntax)

การอ่านค่า

titleElementObject.text

การกำหนดค่า

titleElementObject.text = string

ตัวอย่างการใช้งาน

ตัวอย่างนี้แสดงชื่อเรื่อง (title) ของเอกสารเมื่อกดปุ่ม และนำมาแสดงใน legend ด้วย

<!DOCTYPE html>
<html>
<head>
<title id="titleSelect">HTML DOM Title text</title>
<style>
  form { width:70%; margin: 0 auto; text-align: center; }
  * { padding: 2px; margin:5px; }
  input[type="button"] { border-radius: 10px; }
</style>
</head>
<body>
  <form>
    <fieldset>
      <legend id="legendSelect"></legend>
      <input type="button" onclick="getTitleText()" value="What's the title of document?">
      <div id="divDisplay"></div>
    </fieldset>
  </form>
<script>
  var divDisplay = document.getElementById("divDisplay");
  var legendSelect = document.getElementById("legendSelect");
  var titleSelect = document.getElementById("titleSelect");
  function getTitleText() {
    divDisplay.textContent = 'Title of document: '+titleSelect.text;
    legendSelect.textContent = titleSelect.text.split(' ').join('-');
  }
</script>
</body>
</html>

ผลลัพธ์ (Output)

ก่อนกดปุ่ม "What's the title of document?":

คุณสมบัติ text ของ HTML DOM Title

หลังกดปุ่ม "What's the title of document?":

คุณสมบัติ text ของ HTML DOM Title