你可以使用JavaScript通过按钮点击来切换元素的可见性。

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

How can I use js to toggle the visibility of an element based on a button click?

问题

Sure, here is the translated code:

如果我有隐藏的元素下拉列表),它们在单击父元素时打开那么如何通过单击另一个父元素来关闭它们这是我在js中的代码

```javascript
function toggleElement(elementId) {
  var element = document.querySelector(elementId);
  element.style.display = element.style.display === "none" ? "block" : "none";
}

document.addEventListener("DOMContentLoaded", function() {
  var buttons = document.getElementsByClassName("ContainerForMenu");
  for (var i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener("click", function() {
      var target = this.getAttribute("data-target");
      toggleElement(target);
    });
  }
});

作为解决方案,我尝试了以下选项,但它不起作用

function toggleElement(elementId) {
  var elements = document.querySelectorAll('.child-element');
  
  for (var i = 0; i < elements.length; i++) {
    elements[i].style.display = "none";
  }
  
  var element = document.querySelector(elementId);
  element.style.display = "block";
}

document.addEventListener("DOMContentLoaded", function() {
  var buttons = document.getElementsByClassName("ContainerForMenu");
  
  for (var i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener("click", function() {
      var target = this.getAttribute("data-target");
      toggleElement(target);
    });
  }
});

<details>
<summary>英文:</summary>

if I have hidden elements (dropdown list) that open on click on the parent element, how can I close them by clicking on another parent element in this case? here is the code i have in js

function toggleElement(elementId) {
var element = document.querySelector(elementId);
element.style.display = element.style.display === "none" ? "block" : "none";
}

document.addEventListener("DOMContentLoaded", function() {
var buttons = document.getElementsByClassName("ContainerForMenu");
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function() {
var target = this.getAttribute("data-target");
toggleElement(target);
});
}
});


as a solution i tried this option but it doesn&#39;t work


function toggleElement(elementId) {
var elements = document.querySelectorAll('.child-element');

for (var i = 0; i < elements.length; i++) {
elements[i].style.display = "none";
}

var element = document.querySelector(elementId);
element.style.display = "block";
}

document.addEventListener("DOMContentLoaded", function() {
var buttons = document.getElementsByClassName("ContainerForMenu");

for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function() {
var target = this.getAttribute("data-target");
toggleElement(target);
});
}
});







</details>


# 答案1
**得分**: 1

跟随我的代码,您可以使用切换按钮来根据按钮点击来切换元素的可见性...

```javascript
function myFunction() {
  var x = document.getElementById("myDIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}
<button onclick="myFunction()">试一试</button>

<div id="myDIV">
  这是我的DIV元素。
</div>

这是您要翻译的内容。

英文:

Follow my code you can use toggle toggle the visibility of an element based on a button click...

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

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

function myFunction() {
  var x = document.getElementById(&quot;myDIV&quot;);
  if (x.style.display === &quot;none&quot;) {
    x.style.display = &quot;block&quot;;
  } else {
    x.style.display = &quot;none&quot;;
  }
}

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

#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}

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

&lt;button onclick=&quot;myFunction()&quot;&gt;Try it&lt;/button&gt;

&lt;div id=&quot;myDIV&quot;&gt;
This is my DIV element.
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月23日 01:21:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308587.html
匿名

发表评论

匿名网友

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

确定