简体   繁体   English

如何隐藏元素并在单击时显示新元素?

[英]How to hide an element and show a new element on click?

I have three images which, when clicked, display a hidden element containing text. 我有三个图像,当单击它们时,将显示一个包含文本的隐藏元素。 However, I would like to ensure that the other 2 hidden elements are hidden when I display one. 但是,我想确保在显示一个元素时其他2个隐藏元素被隐藏。 So, if I were to click on #img2, then #text2 would be displayed but #text1 and #text3 would be hidden. 因此,如果我单击#img2,则将显示#text2,但将隐藏#text1和#text3。 Also, this may just be a syntax error but when I tried to put all of my code under the script tag, it wouldn't work correctly. 另外,这可能只是语法错误,但是当我尝试将所有代码放在script标记下时,它将无法正常工作。 I'd also love to place all of the jquery in the script tag rather than using the onclick feature in HTML. 我也很想将所有的jquery放在script标签中,而不是在HTML中使用onclick功能。 I'm very new to jquery so all help is appreciated. 我对jquery非常陌生,因此感谢所有帮助。 Thanks in advance! 提前致谢!

<div id = "img1" onclick = "$('#text1').fadeIn(500)"></div>
<div id = "img2" onclick = "$('#text2').fadeIn(500)"></div>
<div id = "img3" onclick = "$('#text3').fadeIn(500)"></div>

<div id = "text1" hidden></div>
<div id = "text2" hidden></div>
<div id = "text3" hidden></div>

<script>
    /* TODO: Put all on-click code here */
</script>

Its a good practice to keep your code unobstrusive 保持代码简洁的好习惯

HTML 的HTML

<div id = "img1" class="img">One</div>
<div id = "img2" class="img">Two</div>
<div id = "img3" class="img">Three</div>

<div id = "text1" class="hidden">One</div>
<div id = "text2" class="hidden">Two</div>
<div id = "text3" class="hidden">Three</div>​

CSS 的CSS

.hidden{display:none}​

jQuery jQuery的

$(function(){
    $("div.img").on("click", function(){
       var id = $(this).attr("id").substr(-1);
       $(".hidden").hide();
       $("#text"+id).show();        
    });   
})​

DEMO 演示

<div id = "img1" onclick = "$('.effect').hide();$('#text1').show()"></div>
<div id = "img2" onclick = "$('.effect').hide();$('#text2').show()"></div>
<div id = "img3" onclick = "$('.effect').hide();$('#text3').show()"></div>

<div id = "text1" class="effect" hidden></div>
<div id = "text2" class="effect" hidden></div>
<div id = "text3" class="effect" hidden></div>

You can try something like this: 您可以尝试如下操作:

HTML: HTML:

<div id="div1">One</div>
<div id="div2">Two</div>
<div id="div3">Three</div>

<div id="texts">
    <div id="txt1">Text One</div>
    <div id="txt2">Text Two</div>
    <div id="txt3">Text Three</div>
</div>

JavaScript: JavaScript:

$(function(){

    // hide every text div (all texts children)
    $( "#texts" ).children().hide();

    // this function hides every text child and than
    // fades in the desired one
    function hideAndShow( toShowSel ) {
        $( "#texts" ).children().hide();
        $( toShowSel ).fadeIn();
    }

    // registering the click event for the divs...
    $( "#div1" ).click( function(){
        hideAndShow( "#txt1"  );
    });

    $( "#div2" ).click( function(){
        hideAndShow( "#txt2"  );
    });

    $( "#div3" ).click( function(){
        hideAndShow( "#txt3"  );
    });
});

A live example can be found here: http://jsfiddle.net/davidbuzatto/FztUN/ 可以在此处找到一个实时示例: http : //jsfiddle.net/davidbuzatto/FztUN/

http://jsfiddle.net/uptVT/ http://jsfiddle.net/uptVT/

<div id = "img1" onclick = "$('.text').hide();$('#text1').show();">img1</div>
<div id = "img2" onclick = "$('.text').hide();$('#text2').show();">img2</div>
<div id = "img3" onclick = "$('.text').hide();$('#text3').show();">img3</div>

<div id="text1" class = "text" hidden>text1</div>
<div id="text2" class = "text" hidden>text2</div>
<div id="text3" class = "text" hidden>text3</div>

<script>
    /* TODO: Put all on-click code here */
</script>​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM