HTML DOM 菜单对象
htmlweb developmentfront end technology更新于 2025/9/19 10:52:17
HTML 中的 HTML DOM 菜单对象表示 <menu> 元素。
注意 − <menu> 元素仅在 Mozilla Firefox 浏览器中受支持。
语法
以下是语法 −
创建 <menu> 元素
var menuObject = document.createElement(“MENU”)
示例
让我们看一个 HTML DOM 菜单 元素的示例 −
<!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>
输出
这将产生以下输出 −
右键单击表单 −

在上下文菜单中点击"获取网址"菜单项后 −

点击上下文菜单中的"获取主机名"菜单项后 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

