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

คุณสมบัติ HTML DOM Input URL readOnly

คุณสมบัติ readOnly ของ HTML DOM Input URL ใช้กำหนดหรือตรวจสอบว่าช่องป้อน URL สามารถแก้ไขได้หรือไม่ (อ่านอย่างเดียว)

ไวยากรณ์ (Syntax)

การดึงค่า readOnly

inputURLObject.readOnly

การกำหนดค่า readOnly

inputURLObject.readOnly = booleanValue

ค่าบูลีน (Boolean Values)

booleanValueรายละเอียด
Trueช่อง URL อ่านอย่างเดียว - ไม่สามารถแก้ไขได้
Falseช่อง URL แก้ไขได้ (ค่าเริ่มต้น)

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

ตัวอย่าง URL ที่อ่านอย่างเดียว - ให้คัดลอกได้แต่แก้ไขไม่ได้

<!DOCTYPE html>
<html>
<head>
<title>Input URL readOnly</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>URL-readOnly</legend>
    <label for="URLSelect">ติดต่อเรา : <input type="url" id="URLSelect" onclick="showErrorMsg()" value="https://www.google.com" readOnly> </label> 
    <input type="button" onclick="showMessage()" value="Copy URL"> 
    <div id="divDisplay"></div> 
  </fieldset> 
</form> 
<script> 
  var divDisplay = document.getElementById("divDisplay"); 
  var inputURL = document.getElementById("URLSelect"); 
  divDisplay.textContent = 'Above URL is read-only'; 
  function showMessage() { 
    inputURL.select(); 
    document.execCommand('copy'); 
    divDisplay.textContent = 'URL Copied: ' + inputURL.value; 
  } 
  function showErrorMsg(){ 
    divDisplay.textContent += ', This cannot be edited.'; 
  } 
</script> 
</body>
</html>

ผลลัพธ์

เริ่มต้น - แสดง "Above URL is read-only"

คลิกช่อง URL - เพิ่มข้อความ ", This cannot be edited."

คลิก "Copy URL" - เลือก URL อัตโนมัติ คัดลอกไป clipboard แสดง "URL Copied: https://www.google.com"

ประโยชน์: ใช้สำหรับแสดง URL/ลิงก์ที่ต้องการให้ผู้ใช้คัดลอกแต่อย่าปล่อยให้แก้ไข