html导航栏+下拉菜单+表单验证
以下是一个简单的HTML导航栏,带下拉菜单和表单验证的示例代码:
<!DOCTYPE html>
<html>
<head>
<title>导航栏示例</title>
<style>
/* 添加简单的样式 */
.navbar {
overflow: hidden;
background-color: #333;
}
.navbar a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<div class="navbar">
<a href="#home">主页</a>
<a href="#news">新闻</a>
<div class="dropdown">
<button class="dropbtn">下载
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">PDF</a>
<a href="#">EXE</a>
</div>
</div>
<a href="#contact">联系</a>
<a href="#about">关于</a>
</div>
<h2>下拉菜单示例</h2>
<form action="/action_page" onsubmit="return validateForm()">
名字: <input type="text" id="fname" name="fname">
<input type="submit" value="提交">
</form>
<script>
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("名字必须填写");
return false;
}
}
</script>
</body>
</html>
这段代码展示了如何创建一个基本的导航栏,其中包含一个下拉菜单和一个简单的表单验证。当用户尝试提交表单但没有填写名字时,会弹出警告窗口提示用户进行填写。
评论已关闭