jQuery/Javascript - Fade in div onclick fade out another doesn't work

半城伤御伤魂 提交于 2019-12-31 07:15:11

问题


I want to pop up a menu on click of a div and I got it all working in this Fiddle: http://jsfiddle.net/EhtrR/825/ but I cant manage to make it work on my code.

HTML:

<div id="clickOne" class="clickDesign">
<h2 class="fs20 nobold">Leafy Plants</h2>
</div>
<div  id="clickTwo" class="clickDesign">
<h2 class="fs20 nobold">Juicy Plants</h2>
</div>
</div>
<div id="leafyPlants">
Leafy Test
</div>
<div id="juicyPlants">
Juicy Test
</div>

CSS:

    #leafyPlants{
        display:none;
        position:absolute;
        top:50px;
    }

#juicyPlants{
    display:none;
    position:absolute;
    top:50px;
}

jQuery:

  $("#clickOne").on('click', function() {
   $("#leafyPlants").fadeIn();
   $("#juicyPlants").fadeOut();
});
$("#clickTwo").on('click', function() {
   $("#leafyPlants").fadeOut();
   $("#juicyPlants").fadeIn();
});

It doesn't show anything when I put it my code.


回答1:


Add this css

<style>
img {
    float: left;
    display: block;
    height: 40px;
    width: 40px;
    cursor: pointer;
}
#img1 {
    background: #b00;
}
#img2 {
    background: #c00;
}
#div1 {
    display: none;
    position: absolute;
    top: 50px;
    width: 200px;
    background: #077;
    left: 10px;
}
#div2 {
    display: none;
    position: absolute;
    top: 50px;
    width: 200px;
    background: #0b0;
    left: 10px;
}
br {
    clear: both;
}
</style>

This js codes

<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script>
$(document).ready(function(e) {
    $("#img1").on('click', function() {
   $("#div1").fadeIn();
   $("#div2").fadeOut();
});

$("#img2").on('click', function() {
   $("#div1").fadeOut();
   $("#div2").fadeIn();
});
});
</script>

And you HTML

<img id="img1"/> <img id="img2"/> <br/>
<div id="div1">1</div>
<div id="div2">2</div>

It should work. Most probably you did not included jQuery library. I added it.

<script src="http://code.jquery.com/jquery-1.11.0.js"></script>

Using it should solve your porblem probably.



来源:https://stackoverflow.com/questions/23826719/jquery-javascript-fade-in-div-onclick-fade-out-another-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!