使用一个函数和数字参数隐藏和显示元素(Hide and show elements using one function and number argument)

编程入门 行业动态 更新时间:2024-10-27 06:20:27
使用一个函数和数字参数隐藏和显示元素(Hide and show elements using one function and number argument) <script> function gato(n){ if(n == 1){ document.getElementById("A" + n).style.display="block"; } if(n == 2){ document.getElementById("B" + n).style.display="block"; } } </script> <body style="width:auto"> <div class="wrapper"> <div class="cuadro1" onClick="gato(1)"> <div style="display:none" id="A1" onClick="gato(2)"> <img src="images/x.png" alt=""/> </div> <div style="display:none" id="B2" onClick="gato(1)"> <img src="images/o.png" alt=""/> </div> </div> </div>

应该发生的是:当你点击div类cuadro1然后将显示id为A1的div,然后当你点击该div(id A1 )时,将显示div id B2 。 然后你可以点击那个div id B2然后回到div id A1 。

我认为问题在于你必须在用document.getElementById("B2").style.display="none";显示id B2之前取消显示id document.getElementById("B2").style.display="none"; 但我无法让它发挥作用。

<script> function gato(n){ if(n == 1){ document.getElementById("A" + n).style.display="block"; } if(n == 2){ document.getElementById("B" + n).style.display="block"; } } </script> <body style="width:auto"> <div class="wrapper"> <div class="cuadro1" onClick="gato(1)"> <div style="display:none" id="A1" onClick="gato(2)"> <img src="images/x.png" alt=""/> </div> <div style="display:none" id="B2" onClick="gato(1)"> <img src="images/o.png" alt=""/> </div> </div> </div>

What is supposed to happen is: when you click the div class cuadro1 then the div with id A1 is going to be displayed, then when you click that div(id A1) then the div id B2 is going to be displayed. then you can click that div id B2 and go back to div id A1.

I believe the problem lies in that you have to un-display id A before displaying id B2 with document.getElementById("B2").style.display="none"; but I just can't get it to work.

最满意答案

因为你是初学者,我也改变了你的设计:

HTML

<div class="wrapper"> <div class="cuadro1" onClick="gato(1)"> <h1>Click here to show #1</h1> </div> <div> <div style="display:none" id="A1" onClick="gato(2, this)"> <h2>DIV#1</h2> </div> <div style="display:none" id="A2" onClick="gato(1, this)"> <h2>DIV#2</h2> </div> </div> </div>

我把#A1和#A2放在class="cuadro1" 。 这是因为点击事件从点击的元素冒泡到他的父母。 因此,当任何人点击A1或A2 ,也会点击cuadro1 。 这意味着A1将被总是显示出来。

为方便起见,我还将B2重命名为A2 。

使用Javascript

functiongato(n, clickedDiv) { // Hide the div that was clicked if(clickedDiv!=null) clickedDiv.style.display = "none"; // Change whatever ID is given document.getElementById("A" + n).style.display="block"; }

为了节省时间,我们可以在onclick使用this引用来隐藏被单击的div。 如果您不想隐藏单击的div, clickedDiv!=null允许您不传递任何内容。 由于名称现在是A1和A2 ,因此不再需要if语句。

这是一个测试: https : //jsfiddle.net/1zojdvt0/

Because you're a beginner, I also made changes to your design:

HTML

<div class="wrapper"> <div class="cuadro1" onClick="gato(1)"> <h1>Click here to show #1</h1> </div> <div> <div style="display:none" id="A1" onClick="gato(2, this)"> <h2>DIV#1</h2> </div> <div style="display:none" id="A2" onClick="gato(1, this)"> <h2>DIV#2</h2> </div> </div> </div>

I put #A1 and #A2 from class="cuadro1". This is because click events bubble from the clicked element to his parents. So when anyone clicks A1 or A2 the cuadro1 is also clicked. That means A1 would be allways shown.

I also renamed B2 to A2 for convenience.

Javascript

functiongato(n, clickedDiv) { // Hide the div that was clicked if(clickedDiv!=null) clickedDiv.style.display = "none"; // Change whatever ID is given document.getElementById("A" + n).style.display="block"; }

To save ourselves time, we can use this refference in onclick to hide div that was clicked. The clickedDiv!=null allows you to pass nothing if you don't want to hide clicked div. Since names are now A1 and A2, if statement is no longer needed.

Here's a test: https://jsfiddle.net/1zojdvt0/

更多推荐

id,style,display,电脑培训,计算机培训,IT培训"/> <meta name="description&

本文发布于:2023-07-29 20:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319349.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   参数   一个函数   数字   Hide

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!