切换在多个 div 之间显示/隐藏函数

huangapple go评论65阅读模式
英文:

Toggle show/hide functions between multiple divs

问题

以下是您要翻译的内容:

I have a page on my site which has 3 separate 'hidden' divs. Each with it's own 'show/hide' button.

  • Currently... each div and button set functions independently.
  • Therefore... if all divs are shown (open) at the same time, they stack according to their respective order.

Instead of that, I would rather restrict the function a bit, so that only div can be shown (open) at a time.

Example: If Div 1 is shown, and the user then clicks the Div 2 (or Dive 3) button, Div 1 (or which ever div is open at the time, will close.

I am not sure how to adjust my code to make that all work together. I have tried a few ideas, but they were all duds. So I posted a generic 'independent' version below.

以下是您提供的代码部分,不需要翻译:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function show_Div_1() {
  var div1 = document.getElementById("Div_1");
  if (div1.style.display === "none") {
    div1.style.display = "block";
  } else {
    div1.style.display = "none";
  }
}

function show_Div_2() {
  var div2 = document.getElementById("Div_2");
  if (div2.style.display === "none") {
    div2.style.display = "block";
  } else {
    div2.style.display = "none";
  }
}

function show_Div_3() {
  var div3 = document.getElementById("Div_3");
  if (div3.style.display === "none") {
    div3.style.display = "block";
  } else {
    div3.style.display = "none";
  }
}
<!-- language: lang-css -->
.div {
  width: 270px;
  height: 30px;
  padding-left: 10px;
}
<!-- language: lang-html -->
<button type="button" onclick="show_Div_1()">Div 1 - Red</button>
<button type="button" onclick="show_Div_2()" style="margin-left: 4px">Div 2 - Blue</button>
<button type="button" onclick="show_Div_3()" style="margin-left: 4px">Div 3 - Green</button>
<div id="Div_1" class="div" style="background-color:red; display: none;"></div>
<div id="Div_2" class="div" style="background-color:blue; display: none;"></div>
<div id="Div_3" class="div" style="background-color:green; display: none;"></div>
<!-- end snippet -->
英文:

I have a page on my site which has 3 separate 'hidden' divs. Each with it's own 'show/hide' button.

  • Currently... each div and button set functions independently.
  • Therefore... if all divs are shown (open) at the same time, they stack according to their respective order.

Instead of that, I would rather restrict the function a bit, so that only div can be shown (open) at a time.

Example: If Div 1 is shown, and the user then clicks the Div 2 (or Dive 3) button, Div 1 (or which ever div is open at the time, will close.

I am not sure how to adjust my code to make that all work together. I have tried a few ideas, but they were all duds. So I posted a generic 'independent' version below.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function show_Div_1() {
  var div1 = document.getElementById(&quot;Div_1&quot;);
  if (div1.style.display === &quot;none&quot;) {
    div1.style.display = &quot;block&quot;;
  } else {
    div1.style.display = &quot;none&quot;;
  }
}

function show_Div_2() {
  var div2 = document.getElementById(&quot;Div_2&quot;);
  if (div2.style.display === &quot;none&quot;) {
    div2.style.display = &quot;block&quot;;
  } else {
    div2.style.display = &quot;none&quot;;
  }
}

function show_Div_3() {
  var div3 = document.getElementById(&quot;Div_3&quot;);
  if (div3.style.display === &quot;none&quot;) {
    div3.style.display = &quot;block&quot;;
  } else {
    div3.style.display = &quot;none&quot;;
  }
}

<!-- language: lang-css -->

.div {
  width: 270px;
  height: 30px;
  padding-left: 10px;
}

<!-- language: lang-html -->

&lt;button type=&quot;button&quot; onclick=&quot;show_Div_1()&quot;&gt;Div 1 - Red&lt;/button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;show_Div_2()&quot; style=&quot;margin-left: 4px&quot;&gt;Div 2 - Blue&lt;/button&gt;
&lt;button type=&quot;button&quot; onclick=&quot;show_Div_3()&quot; style=&quot;margin-left: 4px&quot;&gt;Div 3 - Green&lt;/button&gt;
&lt;div id=&quot;Div_1&quot; class=&quot;div&quot; style=&quot;background-color:red; display: none;&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;Div_2&quot; class=&quot;div&quot; style=&quot;background-color:blue; display: none;&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;Div_3&quot; class=&quot;div&quot; style=&quot;background-color:green; display: none;&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是代码部分的翻译:

BUTTONS

<button type="button" onclick="show_div('Div_1')">Div 1 - 红色</button>

<button type="button" onclick="show_div('Div_2')" style="margin-left: 4px">Div 2 - 蓝色</button>

<button type="button" onclick="show_div('Div_3')" style="margin-left: 4px">Div 3 - 绿色</button>

SCRIPT

function show_div(div_id) {
    var thisDiv = document.querySelector('#' + div_id);
    var thisState = thisDiv.style.display;
    // 无论如何都关闭所有的
    document.querySelectorAll('.div').forEach(function(el) {
        el.style.display = "none";
    });
    // 只有在它关闭的情况下打开这个 div
    if (thisState == "none") {
        thisDiv.style.display = "block";
    }
}

希望这对你有帮助。

英文:

This can be done in many ways. I think the best approach in your case could be

BUTTONS

&lt;button type=&quot;button&quot; onclick=&quot;show_div(&#39;Div_1&#39;)&quot;&gt;Div 1 - Red&lt;/button&gt;

&lt;button type=&quot;button&quot; onclick=&quot;show_div(&#39;Div_2&#39;)&quot; style=&quot;margin-left: 4px&quot;&gt;Div 2 - Blue&lt;/button&gt;

&lt;button type=&quot;button&quot; onclick=&quot;show_div(&#39;Div_3&#39;)&quot; style=&quot;margin-left: 4px&quot;&gt;Div 3 - Green&lt;/button&gt;

SCRIPT

function show_div(div_id) {
    var thisDiv = document.querySelector(&#39;#&#39;+div_id);
    var thisState = thisDiv.style.display;
    // close all in any cases
    document.querySelectorAll(&#39;.div&#39;).forEach(function(el) {
        el.style.display = &quot;none&quot;;
    });
    // open this div only if it was closed
    if (thisState == &quot;none&quot; ){
        thisDiv.style.display = &quot;block&quot;;
    }
}

答案2

得分: 1

我建议使用数据属性来实现开关功能。为什么呢?因为你可以使用CSS来控制它们,而且不仅可以实现简单的开关,还可以实现多个“值”。

在这个示例中,我实现了你的“单击”操作,还添加了按钮的双击操作,以实现第三种状态。尝试一些单击和双击操作吧!

这可能有点过度,但不仅仅是“切换”功能,例如,你可以使用它来显示诸如交通灯等物体的“状态”。

使用grid布局,并通过添加数据属性值来移动它们,然后双击它以使其在CSS中移动到某个grid-area位置等等。

const hideValues = {
  hide: "hidden",
  show: "showme",
  double: "dblclick"
};

function dblClickHander(event) {
  const targetSelector = event.target.dataset.target;
  const target = document.querySelector(targetSelector);
  const action = target.dataset.hideme == hideValues.double ? hideValues.hide : hideValues.double;
  const toggleTargets = document.querySelectorAll('.toggle-target');
  toggleTargets.forEach(el => {
    el.dataset.hideme = hideValues.hide;
  });
  target.dataset.hideme = action;
}

function toggleEventHandler(event) {
  const targetSelector = event.target.dataset.target;
  const target = document.querySelector(targetSelector);
  const showHide = target.dataset.hideme == hideValues.hide ? hideValues.show : hideValues.hide;
  const toggleTargets = document.querySelectorAll('.toggle-target');
  toggleTargets.forEach(el => {
    el.dataset.hideme = hideValues.hide;
  });
  target.dataset.hideme = showHide;
}

/* 设置按钮上的事件处理程序 */
const options = {
  capture: true
};
/* 我们首先执行这一步,以防止点击事件发生 */
const toggleButtons = document.querySelectorAll('.toggle-button');
toggleButtons.forEach(el => {
  el.addEventListener('dblclick', dblClickHander, options);
});
toggleButtons.forEach(el => {
  el.addEventListener('click', toggleEventHandler, options)
});
.toggle-target {
  width: 270px;
  height: 30px;
  padding-left: 10px;
}

.toggle-target[data-hideme="hidden"] {
  display: none;
}

.toggle-target[data-hideme="showme"] {
  display: block;
}

.toggle-target[data-hideme="dblclick"] {
  display: block;
  border: solid 2px green;
  padding: 1rem;
  opacity: 0.50;
}

.red-block {
  background-color: red;
}

.blue-block {
  background-color: blue;
}

.green-block {
  background-color: green;
}
<button type="button" class="toggle-button" data-target=".red-block">Div 1 - Red</button>
<button type="button" class="toggle-button" data-target=".blue-block">Div 2 - Blue</button>
<button type="button" class="toggle-button" data-target=".green-block">Div 3 - Green</button>
<div class="toggle-target red-block" data-hideme="hidden">red</div>
<div class="toggle-target blue-block" data-hideme="hidden">blue</div>
<div class="toggle-target green-block" data-hideme="hidden">green</div>
英文:

I would suggest using data attributes for a toggle. Why? you can use CSS for them and you can use more than just a toggle - multiple "values".

Here in this example I do your "click" but also added a double click on the button for a third value. Try some clicks and double clicks!

A bit of overkill perhaps but more than just "toggle" for example you could use this to show "states" of things like a stoplight or any number of things.

Use the grid display and move them by just adding a data attribute value and double click it to get it to go (using css) to some grid-area:, things like that.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const hideValues = {
hide: &quot;hidden&quot;,
show: &quot;showme&quot;,
double: &quot;dblclick&quot;
};
function dblClickHander(event) {
const targetSelecor = event.target.dataset.target;
const target = document.querySelector(targetSelecor);
const action = target.dataset.hideme == hideValues.double ? hideValues.hide : hideValues.double;
const toggleTargets = document.querySelectorAll(&#39;.toggle-target&#39;);
toggleTargets.forEach(el =&gt; {
el.dataset.hideme = hideValues.hide;
});
target.dataset.hideme = action;
}
function toggleEventHandler(event) {
const targetSelecor = event.target.dataset.target;
const target = document.querySelector(targetSelecor);
const showHide = target.dataset.hideme == hideValues.hide ? hideValues.show : hideValues.hide;
const toggleTargets = document.querySelectorAll(&#39;.toggle-target&#39;);
toggleTargets.forEach(el =&gt; {
el.dataset.hideme = hideValues.hide;
});
target.dataset.hideme = showHide;
}
/* set up event handlers on the buttons */
const options = {
capture: true
};
/* we do this first to prevent the click from happening */
const toggleButtons = document.querySelectorAll(&#39;.toggle-button&#39;);
toggleButtons.forEach(el =&gt; {
el.addEventListener(&#39;dblclick&#39;, dblClickHander, options);
});
toggleButtons.forEach(el =&gt; {
el.addEventListener(&#39;click&#39;, toggleEventHandler, options)
});

<!-- language: lang-css -->

.toggle-target {
width: 270px;
height: 30px;
padding-left: 10px;
}
.toggle-target[data-hideme=&quot;hidden&quot;] {
display: none;
}
.toggle-target[data-hideme=&quot;showme&quot;] {
display: block;
}
.toggle-target[data-hideme=&quot;dblclick&quot;] {
display: block;
border: solid 2px green;
padding: 1rem;
opacity: 0.50;
}
.red-block {
background-color: red;
}
.blue-block {
background-color: blue;
}
.green-block {
background-color: green;
}

<!-- language: lang-html -->

&lt;button type=&quot;button&quot; class=&quot;toggle-button&quot; data-target=&quot;.red-block&quot;&gt;Div 1 - Red&lt;/button&gt;
&lt;button type=&quot;button&quot; class=&quot;toggle-button&quot; data-target=&quot;.blue-block&quot;&gt;Div 2 - Blue&lt;/button&gt;
&lt;button type=&quot;button&quot; class=&quot;toggle-button&quot; data-target=&quot;.green-block&quot;&gt;Div 3 - Green&lt;/button&gt;
&lt;div class=&quot;toggle-target red-block&quot; data-hideme=&quot;hidden&quot;&gt;red&lt;/div&gt;
&lt;div class=&quot;toggle-target blue-block&quot; data-hideme=&quot;hidden&quot;&gt;blue&lt;/div&gt;
&lt;div class=&quot;toggle-target green-block&quot; data-hideme=&quot;hidden&quot;&gt;green&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 04:59:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定