javascript - 单击图像显示/隐藏多个 div

标签 javascript jquery html

我是编码新手,所以请多多包涵。我查看了针对同一问题发布的一些回答,并在一些已解决的 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  &nbsp;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 因为属性 hash 不存在。

如果我的解决方案抛出类似“undefined method attr()”的错误,您可能需要使用 jQuery 选择器。

$($(this).attr('href')).show();

我建议使用控制台对象进行调试。您可以在 Mozilla Developer Network 上找到一些关于调试的文档。 .

此选择器 $("#output > div") 与此选择器 $("#output div") 不同。根据您的 HTML,两个选择器返回相同的元素,但如果您要更改 HTML 结构,则可能会出现问题。

关于javascript - 单击图像显示/隐藏多个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15446304/

相关文章:

javascript - D3JS - 圆圈上的标签框

jquery - 事件委托(delegate)问题

javascript - 呈现 HTML 文档缩略图

javascript - HTML/Javascript 货币转换器

java - 如何在 JavaScript 警报框中打印 Struts2 ActionMessage

javascript - Polymer 中的嵌套数组观察

Javascript 替换 CKEditor 中的所有值

javascript - 如果图像不存在,则更新 img src

javascript - 使用重置默认值的 jQuery 上传图像预览

javascript - 未捕获的类型错误 : Cannot read property 'ajax' of undefined When I'm trying to send data to url