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

คุณสมบัติ value ของ Li ใน HTML DOM

คุณสมบัติ value ของอีลีเมนต์ <li> ใช้สำหรับอ่านหรือตั้งค่าค่าแอตทริบิวต์ value (ใช้ใน <ol> เพื่อกำหนดหมายเลขลำดับเอง)

ไวยากรณ์ (Syntax)

การอ่านค่า

liObject.value

การตั้งค่า

liObject.value = 'string'

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

ตัวอย่างเดียวกับบทความ Li Object (1004064733) - ปรับลำดับการแข่งขัน:

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM value</title>
<style>
  form { width: 70%; margin: 0 auto; text-align: center; }
  * { padding: 2px; margin: 5px; }
  input[type="button"] { border-radius: 10px; }
  ol { width: 30%; margin: 0 auto; }
</style>
</head>
<body>
<form>
  <fieldset>
    <legend>HTML-DOM-value</legend>
    <h3>Race Result</h3>
    <ol id="race">
      <li>Alex</li>
      <li>Eden</li>
      <li>Samuel</li>
      <li>Zampa</li>
    </ol>
    <input type="button" onclick="rectifyResult()" value="Alex Cheated">
    <div id="divDisplay"></div>
  </fieldset>
</form>
<script>
  var divDisplay = document.getElementById("divDisplay");
  var studentList = document.getElementsByTagName("li");
  var race = document.getElementById("race");
  
  function rectifyResult() {
    studentList[1].value = '1';
    studentList[2].value = '2';
    studentList[3].value = '3';
    race.removeChild(studentList[0]);
    divDisplay.textContent = 'Alex is disqualified';
  }
</script>
</body>
</html>

ผลลัพธ์

ก่อนคลิกปุ่ม "Alex Cheated": 1. Alex, 2. Eden, 3. Samuel, 4. Zampa

หลังคลิกปุ่ม "Alex Cheated": Alex ถูกลบ, Eden=1, Samuel=2, Zampa=3, แสดง "Alex is disqualified"

หมายเหตุ: รูปภาพตัวอย่างจากต้นฉบับไม่ได้รวมอยู่ในเนื้อหานี้