以下是用于创建点击方法的代码。我点击时收到错误,没有任何反应,我不知道出了什么问题。
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8" />
<title>Blueprint: Vertical Icon Menu</title>
<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" type="text/css" href="css/leftmenu.css" />
<link rel="stylesheet" type="text/css" href="flaticon.css" />
<style>
body {position: relative;font-family: 'Lato', Calibri, Arial, sans-serif; color: #47a3da;}
body, html { font-size: 100%; height: 100%; padding: 0; margin: 0;}
a {text-decoration: none;}
a:hover {color: #000;}
#header{height:20%;width: 100%;background-color: #B9F5BB;}
.dis123{width:75%;float:left; height:80%;text-align: left; }
.postleftmen{width:25%;float:left;color:#f0f0f0;}
div.subcte456{text-align: inherit;line-height: 150%;font-size:20px}
div .subcte456 a{display:inline-block;padding:2px 6px;background-color:#47a3da;color:white;border-radius:4px;margin:3px;cursor:default;}
</style>
</head>
<body>
<div id="header">
Head
</div>
<div class="postleftmen">
<ul class="cbp-vimenu">
<li><a href="#" class="flaticon-number41" > select category</a></li>
<li><a href="#" class="flaticon-smart" onClick="mob();
return false;"> mobile</a></li>
<li><a href="#" class="flaticon-pc6" onClick="ele();
return false;"> electronics & computer</a></li>
<li><a href="#" class="flaticon-car95" onClick="veh();
return false;"> vehicle</a></li>
<li><a href="#" class="flaticon-livingroom6" onClick="hme();
return false;"> home & furniture</a></li>
<li><a href="#" class="flaticon-dog37" onClick="pet();
return false;"> pets</a></li>
<li><a href="#" class="flaticon-cd" onClick="bok();
return false;"> books, cd & hobbies</a></li>
<li><a href="#" class="flaticon-black276" onClick="clo();
return false;"> clothing & accessories</a></li>
<li><a href="#" class="flaticon-baby23" onClick="kid();
return false;"> kids & baby</a></li>
<li><a href="#" class="flaticon-bicycle14" onClick="spo();
return false;"> sport & health</a></li>
<li><a href="#" class="flaticon-tools6" onClick="ser();
return false;"> service</a></li>
<li><a href="#" class="flaticon-businessman221" onClick="job();
return false;"> jobs</a></li>
<li><a href="#" class="flaticon-house111" onClick="est();
return false;"> real estate</a></li>
</ul>
</div>
<div class="dis123" style="text-transform: uppercase;">
<div class="stepindi" style="width:400px;height:10px;">
<p class="flaticon-number40" style="font-size:25px;"> SELECT YOUR FORM TYPE</p>
</div>
<div class="subcte456" style="font-size: 15px;position:fixed;width:55%;height:30%;margin-top: 30px;margin-left: 150px;">
<div id="mobi" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Mobile Phones</a>
<p class="flaticon-right127"><a href="#">Tablets</a></p>
<p class="flaticon-right127"><a href="#">Mobile Accessories</a></p></div>
<div id="elec" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Cameras & Accessories</a></p>
<p class="flaticon-right127"><a href="#">TV - Video - Audio</a></p>
<p class="flaticon-right127"><a href="#">Computers - Laptops</a></p>
<p class="flaticon-right127"><a href="#">Computer Accessories</a></p>
<p class="flaticon-right127"><a href="#">Video Games - Consoles</a></p>
<p class="flaticon-right127"><a href="#">Others Electronics</a></p></div>
<div id="vehi" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Cars</a></p>
<p class="flaticon-right127"><a href="#">Motorcycles - Scooters</a></p>
<p class="flaticon-right127"><a href="#">Bicycles</a></p>
<p class="flaticon-right127"><a href="#">Commercial - Transportation</a></p>
<p class="flaticon-right127"><a href="#">Spare Parts - Accessories</a></p>
<p class="flaticon-right127"><a href="#">Tractors - Agricultural Equipments </a></p></div>
<div id="home" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Furniture</a></p>
<p class="flaticon-right127"><a href="#">Decor & Furnishings</a></p>
<p class="flaticon-right127"><a href="#">Fridge - AC - Washing Machine</a></p>
<p class="flaticon-right127"><a href="#">Home & Kitchen Appliances</a></p>
<p class="flaticon-right127"><a href="#">Art - Paintings</a></p>
<p class="flaticon-right127"><a href="#">Other Household Goods</a></p></div>
<div id="pets" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Animals</a></p>
<p class="flaticon-right127"><a href="#">Pet Food - Accessories</a></p>
<p class="flaticon-right127"><a href="#">Pet Care & Services</a></p></div>
<div id="book" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Books & Magazines</a></p>
<p class="flaticon-right127"><a href="#">CDs - DVDs</a></p>
<p class="flaticon-right127"><a href="#">Musical Instruments</a></p>
<p class="flaticon-right127"><a href="#">Coins & Collectibles</a></p>
<p class="flaticon-right127"><a href="#">Handicrafts</a></p></div>
<div id="clot" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Clothes</a></p>
<p class="flaticon-right127"><a href="#">Jewellery</a></p>
<p class="flaticon-right127"><a href="#">Bags & Luggage</a></p>
<p class="flaticon-right127"><a href="#">Footwear</a></p>
<p class="flaticon-right127"><a href="#">Clothing Accessories</a></p>
<p class="flaticon-right127"><a href="#">Watches</a></p></div>
<div id="kids" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Baby Products & Furniture</a></p>
<p class="flaticon-right127"><a href="#">Kids Clothing - Accessories</a></p>
<p class="flaticon-right127"><a href="#">Toys & Games</a></p>
<p class="flaticon-right127"><a href="#">Kids Health - Nutrition</a></p></div>
<div id="spor" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Sport Equipment</a></p>
<p class="flaticon-right127"><a href="#">Gym & Fitness</a></p>
<p class="flaticon-right127"><a href="#">Health & Beauty</a></p></div>
<div id="serv" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Education & Classes</a></p>
<p class="flaticon-right127"><a href="#">Electronics - Computers</a></p>
<p class="flaticon-right127"><a href="#">Maids & Domestic Help</a></p>
<p class="flaticon-right127"><a href="#">Health & Beauty</a></p>
<p class="flaticon-right127"><a href="#">Home Improvements & Repairs</a></p>
<p class="flaticon-right127"><a href="#">Movers & Packers</a></p>
<p class="flaticon-right127"><a href="#">Drivers - Taxi Services</a></p>
<p class="flaticon-right127"><a href="#">Car Wash & Repair</a></p>
<p class="flaticon-right127"><a href="#">Other Services</a></p></div>
<div id="jobs" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Job Offers</a></p>
<p class="flaticon-right127"><a href="#">Resumes</a></p></div>
<div id="real" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Houses - Apartments for Sale</a></p>
<p class="flaticon-right127"><a href="#">Houses - Apartments for Rent</a></p>
<p class="flaticon-right127"><a href="#">Land - Plots For Sale</a></p>
<p class="flaticon-right127"><a href="#">Office – Commercial Space</a></p>
<p class="flaticon-right127"><a href="#">PG & Roommates</a></p>
<p class="flaticon-right127"><a href="#">Vacation Rentals – Guest Houses</a></p></div>
</div></div>
<div style="clear:both"> </div>
<script>
function mob() {
hidemenus();
document.getElementById('mobi').style.display = "block";
}
function ele() {
hidemenus();
document.getElementById('elec').style.display = "block";
}
function veh() {
hidemenus();
document.getElementById('vehi').style.display = "block";
}
function hme() {
hidemenus();
document.getElementById('home').style.display = "block";
}
function pet() {
hidemenus();
document.getElementById('pets').style.display = "block";
}
function bok() {
hidemenus();
document.getElementById('book').style.display = "block";
}
function clo() {
hidemenus();
document.getElementById('clot').style.display = "block";
}
function kid() {
hidemenus();
document.getElementById('kids').style.display = "block";
}
function spo() {
hidemenus();
document.getElementById('spor').style.display = "block";
}
function ser() {
hidemenus();
document.getElementById('serv').style.display = "block";
}
function job() {
hidemenus();
document.getElementById('jobs').style.display = "block";
}
function est() {
hidemenus();
document.getElementById('real').style.display = "block";
}
function hidemenus() {
document.getElementById('mobi').style.display = "none";
document.getElementById('elec').style.display = "none";
document.getElementById('vehi').style.display = "none";
document.getElementById('home').style.display = "none";
document.getElementById('pets').style.display = "none";
document.getElementById('book').style.display = "none";
document.getElementById('clot').style.display = "none";
document.getElementById('kids').style.display = "none";
document.getElementById('spor').style.display = "none";
document.getElementById('serv').style.display = "none";
document.getElementById('jobs').style.display = "none";
document.getElementById('real').style.display = "none";
}
</script>
</body>
答案 0 :(得分:2)
“不要重复自己。”
您不需要12个单独的功能来覆盖每个元素。 这会重复代码并使得更难以排除故障(如您所见),并且难以维护。您必须为每个新项目类别添加新功能!
相反,您可以在循环中为每个菜单<li>
标记添加事件侦听器。在这里,您还可以为<li>
元素提供唯一ID或类。在HTML5中,您还可以添加自定义data-
属性。
(function addEventListeners(){
//Give the <ul> an ID instead of a class to target only those <li> elements.
var menuItems = document.getElementById("cbp-vimenu").getElementsByTagName("li");
var len = menuItems.length;
for (var i=0; i < len; i++){
menuItems[i].addEventListener("click", showMenu);
menuItems[i].setAttribute("id", "menuItem" + i);
menuItems[i].setAttribute("data-itemNum", i);
}
})();
然后创建一个showMenu()
函数,将点击的<li>
与您要显示的div进行比较。你可以使用switch-case方法,或者如果list和div的顺序相同,你可以用数字匹配它们(例如:第3个菜单项 - &gt;第3个子菜单div)。任何一个选项都可以减少代码,从而减少重复性和重复性。更容易排除故障。
选项1,switch-case(JSFiddle):
function showMenu(){
hidemenus();
var selectedItem = this.id;
var divID;
switch (selectedItem){
case "menuItem1":
divID = "mobi";
break;
case "menuItem2":
divID = "elec";
break;
//add more cases here to account for other items
default: return false;
}
document.getElementById(divID).style.display = "block";
};
选项2,匹配列表项和匹配元素数组中的div位置(JSFiddle):
function showMenu(){
hidemenus();
var itemNum = this.getAttribute("data-itemNum");
var menuDivs = document.getElementsByClassName("answer_list");
menuDivs[itemNum].style.display = "block";
};
您可以通过定位hidemenus()
属性来简化class="answer_list"
功能:
function hidemenus() {
var menusToHide = document.getElementsByClassName("answer_list");
var len = menusToHide.length;
for (var i = 0; i < len; i++){
menusToHide[i].style.display = "none";
}
};
顺便说一句,如果你所做的只是改变CSS,你不需要使用嵌套的<a>
标签。您可以在任何元素上放置一个click事件侦听器,并使用CSS作为手形光标以指示它是可单击的。
li:hover{
cursor: pointer;
cursor: hand; /*outdated cross-browser option, but no harm including it*/
}
另外,尽量避免内联CSS和内联JavaScript(如onClick()
) - 维护起来要困难得多。正如您所知,很难对此代码进行故障排除,如果您想要更改其功能,则需要在HTML中编辑20多个位置,在外部JS中编辑2个位置。
好的,最后一点注意事项 - 您的HTML缺少一些结束标记,</html>
,“移动”部分中的结束</p>
标记等。通过HTML验证程序运行它。
答案 1 :(得分:0)
进行了一些编辑检查它现在可以正常工作
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8" />
<title>Blueprint: Vertical Icon Menu</title>
<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" type="text/css" href="css/leftmenu.css" />
<link rel="stylesheet" type="text/css" href="flaticon.css" />
<style>
body {position: relative;font-family: 'Lato', Calibri, Arial, sans-serif; color: #47a3da;}
body, html { font-size: 100%; height: 100%; padding: 0; margin: 0;}
a {text-decoration: none;}
a:hover {color: #000;}
#header{height:20%;width: 100%;background-color: #B9F5BB;}
.dis123{width:75%;float:left; height:80%;text-align: left; }
.postleftmen{width:25%;float:left;color:#f0f0f0;}
div.subcte456{text-align: inherit;line-height: 150%;font-size:20px}
div .subcte456 a{display:inline-block;padding:2px 6px;background-color:#47a3da;color:white;border-radius:4px;margin:3px;cursor:default;}
</style>
</head>
<body>
<div id="header">
Head
</div>
<div class="postleftmen">
<ul class="cbp-vimenu">
<li><a href="#" class="flaticon-number41" > select category</a></li>
<li><a href="#" class="flaticon-smart" onClick="mob();
return false;"> mobile</a></li>
<li><a href="#" class="flaticon-pc6" onClick="ele();
return false;"> electronics & computer</a></li>
<li><a href="#" class="flaticon-car95" onClick="veh();
return false;"> vehicle</a></li>
<li><a href="#" class="flaticon-livingroom6" onClick="hme();
return false;"> home & furniture</a></li>
<li><a href="#" class="flaticon-dog37" onClick="pet();
return false;"> pets</a></li>
<li><a href="#" class="flaticon-cd" onClick="bok();
return false;"> books, cd & hobbies</a></li>
<li><a href="#" class="flaticon-black276" onClick="clo();
return false;"> clothing & accessories</a></li>
<li><a href="#" class="flaticon-baby23" onClick="kid();
return false;"> kids & baby</a></li>
<li><a href="#" class="flaticon-bicycle14" onClick="spo();
return false;"> sport & health</a></li>
<li><a href="#" class="flaticon-tools6" onClick="ser();
return false;"> service</a></li>
<li><a href="#" class="flaticon-businessman221" onClick="job();
return false;"> jobs</a></li>
<li><a href="#" class="flaticon-house111" onClick="est();
return false;"> real estate</a></li>
</ul>
</div>
<div class="dis123" style="text-transform: uppercase;">
<div class="stepindi" style="width:400px;height:10px;">
<p class="flaticon-number40" style="font-size:25px;"> SELECT YOUR FORM TYPE</p>
</div>
<div class="subcte456" style="font-size: 15px;position:fixed;width:55%;height:30%;margin-top: 30px;margin-left: 150px;">
<div id="mobi" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Mobile Phones</a>
<p class="flaticon-right127"><a href="#">Tablets</a></p>
<p class="flaticon-right127"><a href="#">Mobile Accessories</a></p></div>
<div id="elec" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Cameras & Accessories</a></p>
<p class="flaticon-right127"><a href="#">TV - Video - Audio</a></p>
<p class="flaticon-right127"><a href="#">Computers - Laptops</a></p>
<p class="flaticon-right127"><a href="#">Computer Accessories</a></p>
<p class="flaticon-right127"><a href="#">Video Games - Consoles</a></p>
<p class="flaticon-right127"><a href="#">Others Electronics</a></p></div>
<div id="vehi" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Cars</a></p>
<p class="flaticon-right127"><a href="#">Motorcycles - Scooters</a></p>
<p class="flaticon-right127"><a href="#">Bicycles</a></p>
<p class="flaticon-right127"><a href="#">Commercial - Transportation</a></p>
<p class="flaticon-right127"><a href="#">Spare Parts - Accessories</a></p>
<p class="flaticon-right127"><a href="#">Tractors - Agricultural Equipments </a></p></div>
<div id="home" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Furniture</a></p>
<p class="flaticon-right127"><a href="#">Decor & Furnishings</a></p>
<p class="flaticon-right127"><a href="#">Fridge - AC - Washing Machine</a></p>
<p class="flaticon-right127"><a href="#">Home & Kitchen Appliances</a></p>
<p class="flaticon-right127"><a href="#">Art - Paintings</a></p>
<p class="flaticon-right127"><a href="#">Other Household Goods</a></p></div>
<div id="pets" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Animals</a></p>
<p class="flaticon-right127"><a href="#">Pet Food - Accessories</a></p>
<p class="flaticon-right127"><a href="#">Pet Care & Services</a></p></div>
<div id="book" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Books & Magazines</a></p>
<p class="flaticon-right127"><a href="#">CDs - DVDs</a></p>
<p class="flaticon-right127"><a href="#">Musical Instruments</a></p>
<p class="flaticon-right127"><a href="#">Coins & Collectibles</a></p>
<p class="flaticon-right127"><a href="#">Handicrafts</a></p></div>
<div id="clot" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Clothes</a></p>
<p class="flaticon-right127"><a href="#">Jewellery</a></p>
<p class="flaticon-right127"><a href="#">Bags & Luggage</a></p>
<p class="flaticon-right127"><a href="#">Footwear</a></p>
<p class="flaticon-right127"><a href="#">Clothing Accessories</a></p>
<p class="flaticon-right127"><a href="#">Watches</a></p></div>
<div id="kids" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Baby Products & Furniture</a></p>
<p class="flaticon-right127"><a href="#">Kids Clothing - Accessories</a></p>
<p class="flaticon-right127"><a href="#">Toys & Games</a></p>
<p class="flaticon-right127"><a href="#">Kids Health - Nutrition</a></p></div>
<div id="spor" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Sport Equipment</a></p>
<p class="flaticon-right127"><a href="#">Gym & Fitness</a></p>
<p class="flaticon-right127"><a href="#">Health & Beauty</a></p></div>
<div id="serv" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Education & Classes</a></p>
<p class="flaticon-right127"><a href="#">Electronics - Computers</a></p>
<p class="flaticon-right127"><a href="#">Maids & Domestic Help</a></p>
<p class="flaticon-right127"><a href="#">Health & Beauty</a></p>
<p class="flaticon-right127"><a href="#">Home Improvements & Repairs</a></p>
<p class="flaticon-right127"><a href="#">Movers & Packers</a></p>
<p class="flaticon-right127"><a href="#">Drivers - Taxi Services</a></p>
<p class="flaticon-right127"><a href="#">Car Wash & Repair</a></p>
<p class="flaticon-right127"><a href="#">Other Services</a></p></div>
<div id="jobs" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Job Offers</a></p>
<p class="flaticon-right127"><a href="#">Resumes</a></p></div>
<div id="real" style="display:none;z-index:99;" class="answer_list" >
<p class="flaticon-right127"><a href="#">Houses - Apartments for Sale</a></p>
<p class="flaticon-right127"><a href="#">Houses - Apartments for Rent</a></p>
<p class="flaticon-right127"><a href="#">Land - Plots For Sale</a></p>
<p class="flaticon-right127"><a href="#">Office – Commercial Space</a></p>
<p class="flaticon-right127"><a href="#">PG & Roommates</a></p>
<p class="flaticon-right127"><a href="#">Vacation Rentals – Guest Houses</a></p></div>
</div></div>
<div style="clear:both"> </div>
<script>
function mob() {
hidemenus();
document.getElementById('mobi').style.display = "block";
}
function ele() {
hidemenus();
document.getElementById('elec').style.display = "block";
}
function veh() {
hidemenus();
document.getElementById('vehi').style.display = "block";
}
function hme() {
hidemenus();
document.getElementById('home').style.display = "block";
}
function pet() {
hidemenus();
document.getElementById('pets').style.display = "block";
}
function bok() {
hidemenus();
document.getElementById('book').style.display = "block";
}
function clo() {
hidemenus();
document.getElementById('clot').style.display = "block";
}
function kid() {
hidemenus();
document.getElementById('kids').style.display = "block";
}
function spo() {
hidemenus();
document.getElementById('spor').style.display = "block";
}
function ser() {
hidemenus();
document.getElementById('serv').style.display = "block";
}
function job() {
hidemenus();
document.getElementById('jobs').style.display = "block";
}
function est() {
hidemenus();
document.getElementById('real').style.display = "block";
}
function hidemenus() {
document.getElementById('mobi').style.display = "none";
document.getElementById('elec').style.display = "none";
document.getElementById('vehi').style.display = "none";
document.getElementById('home').style.display = "none";
document.getElementById('pets').style.display = "none";
document.getElementById('book').style.display = "none";
document.getElementById('clot').style.display = "none";
document.getElementById('kids').style.display = "none";
document.getElementById('spor').style.display = "none";
document.getElementById('serv').style.display = "none";
document.getElementById('jobs').style.display = "none";
document.getElementById('real').style.display = "none";
}
</script>
javascript html