Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Đối tượng Menu trong HTML DOM

Đối tượng Menu trong HTML DOM đại diện cho phần tử <menu>.

LƯU Ý - Phần tử <menu> chỉ được hỗ trợ trong trình duyệt Mozilla Firefox.

Cú pháp

Tạo một phần tử <menu>:

var menuObject = document.createElement("MENU")

Ví dụ

Dưới đây là ví dụ minh họa cho phần tử HTML DOM Menu:

<!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">URL hiện tại:</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="Lấy URL" onclick="contextFunction(1);"> </menuitem> <menuitem label="Lấy 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>

Kết quả

Ví dụ này sẽ cho kết quả sau:

Khi click chuột phải vào form:

Đối tượng Menu trong HTML DOM

Sau khi nhấn 'Lấy URL' trong menu ngữ cảnh:

Đối tượng Menu trong HTML DOM

Sau khi nhấn 'Lấy Hostname' trong menu ngữ cảnh:

Đối tượng Menu trong HTML DOM