简体   繁体   English

使用Java显示/隐藏DIV内容块

[英]Show / Hide DIV content block using Javascript

Below is complete code I am using to show / hide div block on my page. 以下是我用来在页面上显示/隐藏div块的完整代码。 Currently when I click on particular text heading, it is showing block content of that heading. 当前,当我单击特定的文本标题时,它正在显示该标题的块内容。 Then when I click on another text heading, it is showing another block content of that heading but doesn't close previously opened block. 然后,当我单击另一个文本标题时,它将显示该标题的另一个块内容,但不会关闭先前打开的块。 I want to close opened block whenever I click on another heading. 每当我单击另一个标题时,我都想关闭打开的块。 Please help me. 请帮我。

function viewdetail(divno)
{       
    if(document.getElementById("div_com"+ divno).style.display=="block")
    {
        document.getElementById("div_com"+ divno).style.display="none";
        document.getElementById("a_title"+ divno).title="Click to view details";
    }
    else
    {
        document.getElementById("div_com"+ divno).style.display="block";
        document.getElementById("a_title"+ divno).title="Click to hide details";
    }   
    return true;
}


<table>
<?php 
$int_cnt=1;
while(!$rs_list->eof())
{
?>
    <tr> 
        <td>
        <a name="a<?php print($int_cnt)?>"></a>
            <table>
                <tr>
                    <td><a href="#a<?php print($int_cnt)?>" id="a_title<?php print($int_cnt)?>" onClick="return viewdetail(<?php print($int_cnt)?>);"><?php print($rs_list->fields("title"));?></a></td>
                </tr>
                <tr>
                    <td>
                        <div align="justify" id="div_com<?php print($int_cnt)?>" style="display:none"><table><tr><td>Text will display here</td></tr></table></div>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
<?php 
$rs_list->movenext();
$int_cnt=$int_cnt+1;        
}       
?>
</table>

You may want to try using a class to track which div is currently open, and then use that class to find the previously opened div and close it. 您可能想尝试使用一个类来跟踪当前打开了哪个div,然后使用该类来查找先前打开的div并将其关闭。

Javascript has support in modern browsers for getElementByClassName as you can see at http://caniuse.com/#feat=getelementsbyclassname , and https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName . 您可以在http://caniuse.com/#feat=getelementsbyclassnamehttps://developer.mozilla.org/zh-CN/docs/Web/API/document.getElementsByClassName中看到,Javascript在现代浏览器中支持getElementByClassName

So maybe rewrite the function as: 因此,也许将函数重写为:

function viewdetail(divno)
{      
    if(document.getElementsByClassName("last-opened"))
    {
        document.getElementsByClassName("last-opened").style.display="none";
        document.getElementsByClassName("last-opened").title="Click to view details";

    }
    if(document.getElementById("div_com"+ divno).style.display=="block")
    {
        document.getElementById("div_com"+ divno).style.display="none";
        document.getElementById("a_title"+ divno).title="Click to view details";
    }
    else
    {
        document.getElementById("div_com"+ divno).style.display="block";
        document.getElementById("a_title"+ divno).title="Click to hide details";
        document.getElementById("a_title"+ divno).className = "last-opened";
        // Or this next line if you want to keep previous classes there.
        // document.getElementById("a_title"+ divno).className = document.getElementById("a_title"+ divno).className + "last-opened";
    }   
    return true;
}

I didn't test it, but I think that would work. 我没有测试它,但是我认为那是可行的。

The logic in you function only handles the last clicked div. 函数中的逻辑仅处理最后单击的div。 Therefore you need to A, do as others have sugested and close all B, store the divno in a variable so you know witch one was previously opened and should now be closed C, use JQuery 因此,您需要A,像其他人一样进行总结并关闭所有B,将divno存储在变量中,以便您知道一个巫婆先前已打开,现在应该关闭C,使用JQuery

Note: use jquery give class attribute to your div which is same for all the div and make them hide using css. 注意:使用jquery给您的div提供class属性,该属性对于所有div都是相同的,并使用CSS使其隐藏。
HTML(example): HTML(示例):

<table>
    <tr>
        <td><a href="#" class="myLink" id="1">this is 1</a>
        </td>
    </tr>
    <tr>
        <td>
            <div align="justify" id="div_dom1" class="myClass">
                <table>
                    <tr>
                        <td>Text will display here for 1</td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
    <tr>
        <td><a href="#" class="myLink" id="2">this is 2</a>

        </td>
    </tr>
    <tr>
        <td>
            <div align="justify" id="div_dom2" class="myClass">
                <table>
                    <tr>
                        <td>Text will display here for 2</td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
    <tr>
        <td><a href="#" class="myLink" id="3">this is 3</a>

        </td>
    </tr>
    <tr>
        <td>
            <div align="justify" id="div_dom3" class="myClass">
                <table>
                    <tr>
                        <td>Text will display here for 3</td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
</table>

CSS: CSS:

.myClass{
    display:none
}

JS: JS:

$(document).ready(function () {
    $(".myLink").on('click',function(){
        $(".myClass").hide();
        var divno =  $(this).attr('id');
        $("#div_dom" + divno).show();
    });
});  

JSFIDDLE DEMO JSFIDDLE演示

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

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