简体   繁体   English

切换当前元素的可见性

[英]Toggle visibility of the current element

I'm trying to write a function toggle_active to show the hidden content on a click, and collapse the content again on one more click.我正在尝试编写 function toggle_active以在单击时显示隐藏的内容,并在再次单击时再次折叠内容。 Sadly, it does not work.可悲的是,它不起作用。 Could you help me modify it?你能帮我修改一下吗?

 function toggle_active(this){ var x = this.nextSibling; if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; }; }
 .daccord_b{ display:none; }
 <header class="ca_h" onclick="toggle_active(this);"> <i class="i i-plus ca_hi"></i> Title </header> <div class="had daccord_b">Hidden content</div>

Use method nextElementSibling to return the next element.使用方法nextElementSibling返回下一个元素。 And it is not necessary to use the if {} operator.并且没有必要使用if {}运算符。

Don't use this for arguments in functions.不要在函数中将this用于 arguments。

The more correct way for your task is method toggle() , which your class uses in css .daccord_b .您的任务更正确的方法是方法toggle() ,您的 class 在 ZC7A62 .daccord_b .daccord_b 中使用该方法。

 function toggle_active(el) { var x = el.nextElementSibling; x.classList.toggle("daccord_b"); }
 .daccord_b { display: none; }
 <header class="ca_h" onclick="toggle_active(this);"> <i class="i i-plus ca_hi"></i> Title </header> <div class="had daccord_b">Hidden content</div>

Second solution using style.display .使用style.display的第二种解决方案。

 function toggle_active(el) { var x = el.nextElementSibling; x.style.display = x.style.display === 'block'? 'none': 'block'; }
 .daccord_b { display: none; }
 <header class="ca_h" onclick="toggle_active(this);"> <i class="i i-plus ca_hi"></i> Title </header> <div class="had daccord_b">Hidden content</div>

You're having some syntax errors in this code.您在此代码中有一些语法错误。 First, I suggest you name the function arguments something other than this because this is a reserved keyword in JavaScript.首先,我建议您将 function arguments 命名为除此之外的其他名称,因为thisthis中的保留关键字

Secondly, I recommend consulting with W3School with such simple problems before reaching out, as most of the time, there is a simple solution:)其次,我建议在联系之前向 W3School 咨询这些简单的问题,因为大多数情况下,有一个简单的解决方案:)

Here's a link that solves exactly the problem you're describing.这是一个链接,可以完全解决您所描述的问题。

https://www.w3schools.com/howto/howto_js_collapsible.asp https://www.w3schools.com/howto/howto_js_collapsible.asp

And, here's an example and how you can achieve this:而且,这是一个示例以及如何实现此目的:

 let content = document.getElementById("content"); function handleClick() { if (content.classList.contains("hide")) { content.classList.remove("hide"); } else { content.classList.add("hide"); } }
 .my-content { display: block; }.my-content.hide { display: none; }
 <button onclick="handleClick()">Toggle</button> <div class="my-content" id="content">Hello, some content</div>

EDIT If you decide to introduce jQuery to your project, you can achieve it even with fever lines of code:编辑如果您决定将 jQuery 引入您的项目,即使使用狂热的代码行也可以实现它:

 $("[data-collapse]").on('click', function () { let target = $(this).data('collapse'); $(target).toggle(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button data-collapse="#content">Clicker</button> <div id="content"> My Content </div>

This makes it abstract and reusable, even allowing you to do things like separate containers:这使其抽象且可重用,甚至允许您执行单独容器之类的操作:

 $("[data-collapse]").on('click', function () { let target = $(this).data('collapse'); $(target).toggle(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button data-collapse="#target1">Collapse #1</button> <button data-collapse="#target2">Collapse #2</button> <div id="target1"> <h1>I'm Target #1</h1> </div> <div id="target2"> <h1>I'm target #2</h1> </div>

js:

function toggle_active(id){
      var x = document.getElementById(id);
      if (x.style.display === "none" || x.style.display === "") {
        x.style.display = "block";
      } else {
        x.style.display = "none";
      };
    }

html: html:

    <header class="ca_h" onclick="toggle_active('HiddenContent');">
   toggle
</header>
<div class="had daccord_b" id='HiddenContent'>Hidden content</div>

It is a good practice to use an Event object in you handler function when you can.如果可以,最好在处理程序 function 中使用事件 object。 Please read this post then try fixing your code accordingly: What exactly is the parameter e (event) and why pass it to JavaScript functions?请阅读这篇文章,然后尝试相应地修复您的代码: 参数 e(事件)到底是什么以及为什么将其传递给 JavaScript 函数?

More about Event.currentTarget here: https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget有关Event.currentTarget的更多信息: https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget

 function toggle_active(evt){ var x = evt.currentTarget.nextElementSibling; x.classList.toggle('daccord_b'); }
 .daccord_b { display: none; }
 <header class="ca_h" onclick="toggle_active(event);"> <i class="i i-plus ca_hi"></i> Title </header> <div class="had daccord_b">Hidden content</div>

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

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