ออบเจ็กต์ HTML DOM MenuItem ใน HTML แสดงถึง ธาตุ
หมายเหตุ −
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
การสร้าง
var menuItemObject = document.createElement(“MENUITEM”)
คุณสมบัติ
ที่นี่ menuItemobject สามารถมีคุณสมบัติดังต่อไปนี้ -
| คุณสมบัติ | คำอธิบาย |
|---|---|
| ตรวจสอบแล้ว | ตั้งค่า/ส่งคืน (จริง/เท็จ) หากควรตรวจสอบรายการเมนู |
| คำสั่ง | ตั้งค่า/คืนค่าของแอตทริบิวต์คำสั่ง |
| ค่าเริ่มต้น | ตั้งค่า/ส่งคืนว่า menuItem ควรเป็นค่าเริ่มต้นหรือไม่ |
| ปิดการใช้งาน | ตั้งค่า/ส่งคืนว่ารายการเมนูควรปิดใช้งานหรือไม่ |
| ไอคอน | ตั้งค่า/ส่งคืนภาพที่แสดงถึงรายการเมนู |
| ฉลาก | ตั้งค่า/คืนค่าแอตทริบิวต์ label ของ menuItem |
| เรดิโอกรุ๊ป | ตั้งค่า/คืนค่าของแอตทริบิวต์ radiogroup ของ menuItem |
| พิมพ์ | ตั้งค่า/คืนค่าของแอตทริบิวต์ type ของรายการเมนู |
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับ HTML DOM MenuItem องค์ประกอบ −
<!DOCTYPE html>
<html>
<head>
<title>Menu Object</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 contextmenu="MENU">
<fieldset>
<legend>Menu-Object</legend>
<label for="urlSelect">Current URL:</label>
<input type="url" size="30" id="urlSelect" value="https://www.example.com/aboutUs">
<div id="divDisplay"></div>
</fieldset>
<menu type="context"
id="MENU">
<menuitem label="Get URL"
onclick="contextFunction(1);">
</menuitem>
<menuitem label="Get Hostname"
onclick="contextFunction(2);">
</menuitem>
</menu>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var urlSelect = document.getElementById("urlSelect");
function gethref(){
divDisplay.textContent = 'URL Path: '+location.href;
}
function getHostname(){
divDisplay.textContent = 'Hostname: '+location.hostname;
}
function contextFunction(role){
if(role === 1)
this.gethref();
else
this.getHostname();
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
คลิกขวาที่แบบฟอร์ม −

หลังจากคลิก 'รับ URL' รายการเมนูในเมนูบริบท −

หลังจากคลิก 'รับชื่อโฮสต์' รายการเมนูในเมนูบริบท −
