在图像上单击显示/隐藏多个div

时间:2013-03-16 06:29:04

标签: 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();

2 个答案:

答案 0 :(得分:3)

而不是

$(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结构,可能会出现问题。

答案 1 :(得分:0)

尝试将modernizr,jquery和plugin脚本放在代码之前,这意味着改变它:

<script src="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>

到此:

<script src="js/vendor/jquery-1.9.0.min.js"></script>
<script src="js/plugins.js"></script>
<script src="js/vendor/modernizr-2.6.2.min.js"></script>
<script src="js/spicelovelife.js"></script>

希望得到这个帮助。