为什么当我单击导航按钮时 jQuery fadeIn 和 fadeOut 不起作用?

单击此导航按钮后,不会显示“about-goal”中的内容。我需要在单击“关于我”按钮时显示“关于目标”内容,并且还需要在单击“主页”按钮时在同一页面上显示主页。请帮忙。


PS:我是初学者,只是想创建我的第一个网站。非常感谢!


    $(document).ready(function(){

                $('a').click(function(){

                    //alert("i am click");

                    var selected = $(this);

                    $('a').removeClass('active');

                    $(selected).addClass('active');


                });


                var $(a) = $('.a'),

                $b = $('.b'),

                $c = $('.c'),

                $d = $('.d'),

                $home = $('.home'),

                $aboutGoal = $('.aboutGoal');



                $a.click(function(){

                    $home.fadeIn();

                    $aboutGoal.fadeOut();


                });


                $b.click(function(){

                    $home.fadeOut();

                    $aboutGoal.fadeIn();


                });

            }); 

<ul>

  <li class="a"><a href="#">Home</a></li>

  <li class="b"><a href="#">About Me</a></li>

  <li class="c"><a href="#">My Gallery</a></li>

  <li class="d"><a href="#">Contact Me</a></li>

</ul>


<div class="aboutGoal">


  <div class="about-me">


    <h2>MY NAME IS...</h2>

    <p class="p1">My name is... , 23 yrs. Old. I’m an aspiring web developer who loves everything about the web. I've lived in lots of different places and have worked in lots of different jobs. I’m excited to bring my life experience to the process of building fantastic

      looking websites.</p>

    <p class="p2">I’ve been a Service Desk Engineer in IBM and am a life-long learner who's always interested in expanding my skills.</p>



  </div>



慕婉清6462132
浏览 27回答 1
1回答

犯罪嫌疑人X

首先你有一个拼写错误,将var $(a) = $('.a')其更改为var $a = $('.a'),您不需要为每个链接创建点击事件你可以做这样的事情为每个链接提供一个PageSection属性,将其值设置为要隐藏/显示的部分的类也给它相同的类,.nav这样我们就可以只编写一个点击事件<li&nbsp;pageSection="aboutGoal"&nbsp;class="nav">将每个页面部分 div 放入容器 div 中,以便我们可以在单击链接时将它们全部淡出<div&nbsp;id="Pages">然后使用这个点击事件$(document).ready(function () {&nbsp; //view only home section first time&nbsp; $("#Pages").children().hide();&nbsp; $(".Home").show();&nbsp; //when clicking&nbsp; on a&nbsp; li element with class nav&nbsp; $("li.nav").click(function () {&nbsp; &nbsp; //fadout every div&nbsp; inside Pages div&nbsp; &nbsp; $("#Pages").children().fadeOut();&nbsp; &nbsp; // FadeIn&nbsp; element with class&nbsp; is the same name as the pageSection property from the Li we clicked&nbsp; &nbsp; $("." + $(this).attr("pageSection")).fadeIn();&nbsp; &nbsp; //remove active class for every li element with class nav&nbsp; &nbsp; $("li.nav").removeClass("active");&nbsp; &nbsp; //add active class for the li element we clicked&nbsp; &nbsp; $(this).addClass("active");&nbsp; });});实例:&nbsp;https ://codepen.io/vkv88/pen/gOaLgrj?editors=0010
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5