简体   繁体   English

如果打开,则关闭div;如果关闭,则打开

[英]Close div if it is opened or open if it is close

I try to implement below code to my project. 我尝试将以下代码实现到我的项目中。 But also I want to add an extra. 但我也想补充一点。 If I click to button on opened(active) div It should close again. 如果单击打开的(活动)div上的按钮,它应该再次关闭。 I'm doing this in a foreach loop. 我正在foreach循环中执行此操作。 I 'll have many #content so it must be a class like .content . 我将有许多#content因此它必须是.content类的类。

What should I add or change on this nice code block? 我应该在这个漂亮的代码块上添加或更改什么?

All things needed below. 下面需要的所有东西。

http://jsfiddle.net/ardeezstyle/4FKCa/ http://jsfiddle.net/ardeezstyle/4FKCa/

I replace some of your js. 我替换了您的一些js。 Now the buttons have an data-target attribute. 现在,按钮具有数据目标属性。 You have to put a css selector in this attribute. 您必须在该属性中放置一个CSS选择器。 When you click on a button, the js will use the data-target attribute to select the content that needs to be displayed. 当您单击按钮时,js将使用data-target属性选择需要显示的内容。

$("table .load-content").on("click", function () {
    var currentRow = this.parentNode.parentNode;
    if ($(this).hasClass('content-loaded')) {
        $(currentRow).next().toggleClass('hidden');
    } else {
        var content = "<tr class='loaded-content'><td colspan='5'>" + $(this.getAttribute('data-target')).html() + "</td></tr>";
        $(content).insertAfter(currentRow);
        $(this).addClass('content-loaded');
    }
});

Here is the complete code : http://jsfiddle.net/LLaaqqvr/5/ 这是完整的代码: http : //jsfiddle.net/LLaaqqvr/5/

Why just not put the content you want to display directly in the table ? 为什么不将要显示的内容直接放在表中? It will be really easier. 真的会更容易。

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

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