点击图片显示/隐藏多个div

4

我是编码新手,所以请多包容。我看了一些类似问题的解答,通过在一些已解决的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();

你能为这个创建链接吗? - PSR
抱歉,我完全不懂这个。你是指网页链接吗? - PortionsForFoxes
我不明白你的问题,请你能清楚地解释一下吗? - PSR
<div>标签没有隐藏吗?没有显示吗?你看到的问题是什么? - Dan Nissenbaum
你能否在 plunker 上制作一个可工作的示例? - Yahya KACEM
抱歉如果我没有解释清楚。菜单输出中的div没有隐藏。我的意思是,当我点击浏览菜单部分中的一个img时,正确的div将显示出来。在点击浏览菜单部分中的另一个img时,当前的div将隐藏,新的div将显示出来。这是plunker链接:http://plnkr.co/edit/MT4zCZsN3UAzqhvr1yLA - PortionsForFoxes
2个回答

3

不是

$(this.hash).show();

您应该使用:
$(this.attr('href')).show();

this.hash 可能会返回 undefined,因为 hash 属性不存在。

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

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

我建议使用控制台对象进行调试。您可以在Mozilla开发者网络上找到一些有关调试的文档。
这个选择器$("#output > div")与这个选择器$("#output div")不同。根据您的HTML,两个选择器返回相同的元素,但如果更改HTML结构,可能会出现问题。

嗨,Thomas!感谢你回答我的问题。我已经按照你的建议将代码更改为"$($(this).attr('href')).show();",但问题仍然存在。我已经在Chrome中尝试了JavaScript控制台,唯一出现的问题是: Uncaught TypeError: Object [object Object] has no method 'waitForImages'"感谢提供资源! - PortionsForFoxes
1
好的,这是你的第一个问题,你必须解决这个错误,否则代码的其余部分将无法初始化。在调用waitForImages时它不存在。确保你的JavaScript文件按正确顺序排列,并将你的代码包装在ready事件中。现在,你应该注释掉那部分并让我知道它是否有效。 - Thomas Potaire
您,先生,真是太棒了。非常非常感谢!我已经卡在这里两天了哈哈哈。'waitForImages' 是我找到的一个用于预加载页面上所有图像的代码。它可能已经过时或不正确。 - PortionsForFoxes

0
尝试将modernizr、jquery和插件脚本放在你的代码之前,也就是说改成这样:
<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>

希望这可以帮到你。


太好了!谢谢你的帮助 =)我一直害怕来这些论坛,因为我担心人们会嘲笑我的新手。你们真的很有帮助! - PortionsForFoxes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接