javascript – 在图像点击时显示/隐藏多个div
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 在图像点击时显示/隐藏多个div,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9706字,纯文字阅读大概需要14分钟。
内容图文
我对编码很新,所以请耐心等待.我已经查看了针对同一个问题发布的一些答案,并且在一些已解决的jsfiddle链接中输入我的信息时已经获得了代码.
问题是:当我将工作代码插入我的HTML和JS文件时,它似乎不起作用.我查看了我的代码并且不确定是什么打破了它或为什么它没有显示.我想知道是否有人可以查看我的代码并告诉我为什么?
我正在讨论的部分是浏览菜单和菜单输出部分.基本上,如果您在浏览菜单导航中单击图像(杂食,素食,健康),将显示相应的菜单输出.
我还在这篇文章中包含了我的HTML和JS.风格和间距现在有点混乱,因为我一直在修补我的标记中的东西.我最近也看到表格很糟糕,所以我将在我的html中将其更改为无序列表.
非常感谢!
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Spice Up Your Love Life: An Interactive Dating Experience</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="keywords" content="date, interactive, cooking, culinary, dating, couple, love, date ideas, vancouver, spice, love life, experience">
<meta name="Spice Up Your Love Life" content="SpiceUpYourLoveLife.com is an interactive dating experience in which you and your date can cook a meal together. Eat your heart out!">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style2.css">
<link href='http://fonts.googleapis.com/css?family=Rokkitt:400,700' rel='stylesheet' type='text/css'>
<style type="text/css">
@font-face{
font-family: "talldark";
src: url('http://fontsforweb.com/public/fonts/383/talldark.eot');
src: local("Tall Dark And Handsome Condensed"), url('http://fontsforweb.com/public/fonts/383/talldark.woff') format("woff"), url('http://fontsforweb.com/public/fonts/383/talldark.ttf') format("truetype");
}
.diettitle {
font-family: "talldark";
font-size: 6em;
letter-spacing: 5px;
color: #b5a839;
}
.testerz {
font-family: "talldark";
font-size: 1em;
letter-spacing: 4px;
color: #b5a839;
}
</style>
</head>
<body>
<header>
Spice Up Your Love Life: An Interactive Dating Experience
</header>
<div id="topnavcontainer">
<table id="topnav">
<tr>
<td id="break"><a href="google.ca">Breakfast</a></td>
<td id="lunch"><a href="google.ca">Lunch</a></td>
<td id="dinner"><a href="google.ca">Dinner</a></td>
<td id="appetizer"><a href="google.ca">Appetizer</a></td>
<td id="desert"><a href="google.ca">Desert</a></td>
<td id="snack"><a href="google.ca">Snack</a></td>
</tr>
</table>
</div>
<table id="mainnav">
<tr>
<td id="home" class="bordernav"><a href="google.ca">Home</a></td>
<td id="works" class="bordernav"><a href=".waves">How It Works</a></td>
<td id="menu" class="bordernav"><a href="#diet">Menu</a></td>
<td id="mainnavbordernone"></td>
<td id="tips" class="bordernav"><a href="google.ca">Tips and Tricks</a></td>
<td id="contact" class="bordernav"><a href="google.ca">Test Skills</a></td>
<td id="share" class="bordernav"><a href="google.ca">Share</a></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td id="logo"><a href="test.html"><img src="images/logo.png" alt="Spice Up Your Love Life Logo" width="189" height="170"></a></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td id="spicetitle" class="title">SPICE</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td id="upyourtitle" class="title">UP YOUR</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td id="lovelifetitle" class="title">LOVE LIFE</td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<div class="container">
<!--SPLASH CONTENT-->
<div class="content">
<div id="leftgrid">
<img src="images/block.jpg" width="300" height="184"><br>
<p class="contentheader">HOW DOES IT WORK?</p><br>
<p class="contentdescribed">Does it sound too good to be true? --- Well, it's not! See how it works and and how it all started. </p>
<p class="contentcalltoaction"><a href=".waves">See how it works</a></p>
</div>
<div id="middleleftgrid">
<img src="images/block.jpg" width="300" height="184"><br>
<p class="contentheader">WHAT'S FOR DINNER?</p><br>
<p class="contentdescribed">Eat your heart out! Browse our menu and select what
you'd like to cook for your special date. </p>
<p class="contentcalltoaction"><a href="#diet">Look at the menu</a></p>
</div>
<div id="middlerightgrid">
<img src="images/block.jpg" width="300" height="184"><br>
<p class="contentheader">IMPRESS YOUR DATE!</p><br>
<p class="contentdescribed">Don't get caught with your elbows on the table!
These tips will have them wanting seconds! </p>
<p class="contentcalltoaction"><a href="google.ca">Learn tips and tricks</a></p>
</div>
<div id="rightgrid">
<img src="images/block.jpg" width="300" height="184"><br>
<p class="contentheader">GOT WHAT IT TAKES?</p><br>
<p class="contentdescribed">Put your skills to the test! Take our multiple choice test to see what culinary level you're at!</p>
<p class="contentcalltoaction"><a href="google.ca">Test your skills</a></p>
</div>
</div>
<!-- HOW IT WORKS SECTION -->
<div id="howitworkssec">
<div class="waves"></div>
<h3 class="headliners"><strong>How does it work?</strong></h3>
<img src="images/silboth.png" alt="dating silhouette" width="875" height="499">
</div>
<div id="howdesdiv">
<p id="simpleas">It's as simple as 1, 2, 3.</p>
<p id="howdes">
1. Do this thing and that blah blah<br>
2. Then you do this lolol can you believez it?<br>
3. I'll add the actual content when the site is finished.<br>
</p>
<a href="#diet" alt="Browse Menu">Let's begin!</a>
</div>
<!--BROWSE MENU-->
<div id="diet">
<h3 id="meal" class="headliners">Meal Type</h3>
<p id="lefty" class="curly">{</p>
<p id="righty" class="curly">}</p>
<div class="dietchoice"><a href="#meat"><img src="images/carnivoreb.png" alt="meat options" width="90" height="93" /><h3 class="testerz">Omnivore</h3></a></div>
<div class="dietchoice"> <a href="#veggie"><img src="images/vegetarianb.png" alt="meat options" width="90" height="93" /><h3 class="testerz">Vegetarian</h3></a> </div>
<div class="dietchoice"> <a href="#healthy"><img src="images/healthyb.png" alt="meat options" width="90" height="34" /><h3 class="testerz">Healthy</h3></a> </div>
<!--MENU OUTPUT-->
<div id="output">
<div id="meat">
<p class="diettypeheading">Magnificent Meat Dishes</p>
<img src="images/centermeatballs.jpg" alt="meatballsplash" width="600" height="400" id="meatleft" >
<img src="images/centerchicken.jpg" alt="meatballsplash" width="533" height="400" id="meatcenter">
<img src="images/centersteak.jpg" alt="meatballsplash" width="529" height="400" id="meatright">
</div>
<div id="veggie">
<p class="diettypeheading">Fresh, Colourful Dishes</p>
<img src="images/centermeatballs.jpg" alt="meatballsplash" width="600" height="400" id="meatleft" >
<img src="images/meatballs.jpg" alt="meatballsplash" width="533" height="400" id="meatcenter">
<img src="images/centersteak.jpg" alt="meatballsplash" width="529" height="400" id="meatright">
</div>
<div id="healthy">
<p class="diettypeheading">Diabetic and Heart Healthy Meals</p>
<img src="images/centermeatballs.jpg" alt="meatballsplash" width="600" height="400" id="meatleft" >
<img src="images/centerchicken.jpg" alt="meatballsplash" width="533" height="400" id="meatcenter">
<img src="images/meatballs.jpg" alt="meatballsplash" width="529" height="400" id="meatright">
</div>
</div>
<!--END OF CONTENT-->
<div class="splashimage">
<img src="images/banner3.png" alt="splash image" width="1664" height="400">
</div>
<div id="sitemap">
<table class="sitemaptab">
<tr id="sitemaptitles">
<th>Get Social</th>
<th>Quick Links</th>
<th>Terms of Use</th>
</tr>
<tr id="row_two">
<td class="social"><a href="twitter.com"><img src="images/socialmedia/twit_sm.png" alt="sm-twiticon">Follow Us On Twitter</a></td>
<td class="quicklinks"><a href="#topnavcontainer">Home</a></td>
<td class="terms"><a href="google.ca">Privacy Policy</a></td>
</tr>
<tr id="row_three">
<td class="social"><a href="facebook.com"><img src="images/socialmedia/fb_sm.png" alt="sm-fbicon">Like Us On Facebook</a></td>
<td class="quicklinks"><a href="#menudiv">Browse Menu</a></td>
<td class="terms"><a href="google.ca">Contact</a></td>
</tr>
<tr>
<td> </td>
<td class="quicklinks"><a href=".waves">How It Works</a></td>
</tr>
<tr>
<td></td>
<td class="quicklinks"><a href="google.ca">Tips and Tricks</a></td>
</tr>
<tr>
<td></td>
<td class="quicklinks"><a href="google.ca">Share</a></td>
</tr>
</table>
</div>
<footer>
<table id="footer">
<tr>
<td id="copyright">Copyright ? Spice Up Your Love Life 2013</td>
<td id="webdesby">Web Design By: <a href="http://google.ca">Ticking and Talking Media</a></td>
</tr>
</table>
</footer>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="js/spicelovelife.js"></script>
<script src="js/plugins.js"></script>
<script src="js/vendor/jquery-1.9.0.min.js"></script>
<script src="js/vendor/modernizr-2.6.2.min.js"></script>
</body>
</html>
JavaScript的:
// Scrolling Function
$('a').click(function(){
$('html, body').animate({
scrollTop: $( $(this).attr('href') ).offset().top
}, 500);
return false;
});
// Load All Images
$('body').waitForImages({
waitForAll: true,
finished: function() {
// All images have loaded.
}
});
//Hide All Divs
$("#output > div").hide();
$(".dietchoice a").click(function(e) {
$("#output div").hide();
$(this.hash).show();
e.preventDefault();
});
$("#areas div:not(#meat)").hide();
解决方法:
代替
$(this.hash).show();
你可能应该使用
$(this.attr('href')).show();
this.hash可能返回undefined,因为属性哈希不存在.
如果我的解决方案抛出“undefined method attr()”之类的错误,您可能需要使用jQuery选择器.
$($(this).attr('href')).show();
我建议使用控制台对象进行调试.您可以在Mozilla Developer Network上找到一些有关调试的文档.
此选择器$(“#output> div”)与此选择器$(“#output div”)不同.根据您的HTML,两个选择器都返回相同的元素,但如果您要更改HTML结构,则可能会出现问题.
内容总结
以上是互联网集市为您收集整理的javascript – 在图像点击时显示/隐藏多个div全部内容,希望文章能够帮你解决javascript – 在图像点击时显示/隐藏多个div所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。