【发布时间】:2018-02-02 06:20:05
【问题描述】:
我想在鼠标悬停时打开下拉菜单,然后在鼠标悬停时关闭它。我可以打开下拉菜单但不能关闭下拉菜单。
我的代码是,
在header.php文件中,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="http://localhost/performance/css/header.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<?php
include_once 'Connection.php';
session_start();
echo '<body>
<div class="fixed-header">
<div ="main-div">
<nav>
<a href="http://localhost/Performance/Project_form.php" target=>Create Project</a>
<button href="" onmouseover="myFunction1()" class="dropbtn1">Project</button>
<a href="http://localhost/performance/Create_test.php">Create Test</a>
<button href="" onmouseover="myFunction()" class="dropbtn">Test</button>
<div id="nav">
<div id="myDropdown" class="dropdown-content">';
$selected_val=null;
$test_name=null;
function loadtest_name(){
$test=array();
$sql="select loadtest from create_load_test;";
$query=mysqli_query($GLOBALS['db'],$sql);
while ($row = mysqli_fetch_array($query)) {
$test[]=$row['loadtest'];
}
return $test;
}
//Get file name
function load_file_name(){
$file=array();
$sql1="select file_name from type_of_performance_test;";
$query1=mysqli_query($GLOBALS['db'],$sql1);
while ($row = mysqli_fetch_array($query1)) {
$file[]=$row['file_name'];
}
return $file;
}
//Get count of number of test_types
$sql_count="select count(loadtest) from create_load_test;";
$query_count=mysqli_query($GLOBALS['db'],$sql_count);
$row=mysqli_fetch_array($query_count);
$testcount=$row['count(loadtest)'];
for($i=0;$i<$testcount;$i++){
//calling test_name() method and storing those values in a string
$test=loadtest_name();
$test_name=$test[$i];
//calling file_name() method and storing those values in a string
$file=load_file_name();
$file_name=$file[$i];
echo' <a type="submit" name="testname" href='.$file_name.'>'.$test_name.'</a><br>';
}
echo '</div>
</div>
<div id="nav1">
<div id="myDropdown1" class="dropdown-content1">';
$selected_val=null;
$test_name=null;
function project_name(){
$test=array();
$sql="select project_name from project;";
$query=mysqli_query($GLOBALS['db'],$sql);
while ($row = mysqli_fetch_array($query)) {
$test[]=$row['project_name'];
}
return $test;
}
//Get file name
function project_file_name(){
$file=array();
$sql1="select file_name from type_of_performance_test;";
$query1=mysqli_query($GLOBALS['db'],$sql1);
while ($row = mysqli_fetch_array($query1)) {
$file[]=$row['file_name'];
}
return $file;
}
//Get count of number of test_types
$sql_count="select count(project_name) from project;";
$query_count=mysqli_query($GLOBALS['db'],$sql_count);
$row=mysqli_fetch_array($query_count);
$testcount=$row['count(project_name)'];
for($i=0;$i<$testcount;$i++){
//calling test_name() method and storing those values in a string
$test=project_name();
$test_name=$test[$i];
//calling file_name() method and storing those values in a string
$file=project_file_name();
$file_name=$file[$i];
echo' <a type="submit" name="testname1" href='.$file_name.'>'.$test_name.'</a><br>';
}
echo '</div>
</div>
</nav>
</div>
</div>
<div class="fixed-footer">
<div class="container">Merahkee Tech Souluctions,Hubli</div>
</div>';
?>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onmouseover = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
function myFunction1() {
document.getElementById("myDropdown1").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onmouseover = function(event) {
if (!event.target.matches('.dropbtn1')) {
var dropdowns1 = document.getElementsByClassName("dropdown-content1");
var j;
for (j = 0; j < dropdowns1.length; j++) {
var openDropdown = dropdowns1[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
</body>
</html>
在header.css文件中,
body{
padding-top: 0px;
padding-bottom: 40px;
}
.fixed-header, .fixed-footer{
width: 100%;
position: fixed;
background: #333;
padding: 6px 0;
color: #fff;
}
.fixed-header{
top: 0;
}
.fixed-footer{
bottom: 0;
}
.container{
width: 80%;
margin: 0 auto; /* Center the DIV horizontally */
}
nav a{
color: #fff;
text-decoration: none;
padding: 7px 50px;
display: inline-block;
text-decoration:none
}
.dropbtn1 {
background-color:black ;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn1:hover, .dropbtn1:focus {
background-color: ;
}
.fixed-header {
position: relative;
display: inline-block;
}
.dropdown-content1 {
display: none;
position: absolute;
background-color: ;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
margin-left: 4 cm
display:inline-block;
}
.dropdown-content1 a {
color: black;
padding: 18px 16px;
text-decoration: none;
display: block;
margin-left: 4 cm
}
.fixed-header a:hover {background-color: #f1f1f1}
.container{
width: 80%;
margin: 0 auto; /* Center the DIV horizontally */
}
#nav1{margin-left: 5cm}
.dropbtn {
background-color:black ;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: ;
}
.dropdown-content {
display: none;
position: absolute;
background-color: ;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
margin-left: 4 cm
display:inline-block;
}
.dropdown-content a {
color: black;
padding: 18px 16px;
text-decoration: none;
display: block;
margin-left: 4 cm
}
#nav{margin-left: 10cm}
.show {display:block;}
鼠标移出时如何关闭下拉菜单,我知道通过使用 css 我们可以轻松完成。但是这里我不知道如何添加 css。以前,我用这个来做其他的,但是这里我不知道该怎么做。
你能帮帮我吗?
【问题讨论】:
-
尝试使用 CSS
:hover伪类。
标签: javascript jquery html css