简体   繁体   English

javascript显示和隐藏div元素

[英]javascript display and hide div element

I am new to Javascript, and I currently have an article that is being fetched from database, the article has two rows. 我是Java语言的新手,目前有一篇文章是从数据库中获取的,该文章有两行。 title & content there are about 100 of these in my database. 标题内容我的数据库中大约有100个。 Now the objective is to list all the titles first, and when a user clicks on a title, to make the the relevant content appear underneath it. 现在的目标是首先列出所有标题,并在用户单击标题时使相关内容显示在标题下方。 I can do this however this way. 但是我可以这样做。

<?php
//mysql query here...
foreach($result as $row) { ?>
<div id='title'> <?= $row['title'] ?> </div>
<div id='<?= $row['id'] ?>' style='display:none' 
             onclick=showContent(<?= $row['id'] ?>) > <?= $row['content'] ?> 
</div>
<?php } ?>

The javascript to hide the show the content is this: 隐藏显示内容的javascript是这样的:

<script type='text/javascript'>
function showContent(id){
    document.getElementById(id).style.display='inline';
}
</script>

The showContent() function hides the div based on the id passed through the paramenter. showContent()函数根据通过参数的id来隐藏div。 But, the only problem is that, I need other previously displayed divs to truntate when a new one opens. 但是,唯一的问题是,当新的div打开时,我需要其他先前显示的div截断。

Meaning, the content should be visible only once, then when you click on another title, the previously opened content should disappear and only the new content should appear. 意思是,该内容应该仅可见一次,然后当您单击另一个标题时,以前打开的内容应消失,而只有新内容出现。

I hope that made sense. 我希望这是有道理的。 as I am lacking the grammar to explain it all. 因为我缺乏语法来解释所有这一切。 I tried to give small example here, which for some reason does not seem to work at all, but does in my localhost http://jsfiddle.net/YL6aH/ 我尝试在此处举一个小示例,由于某种原因,该示例似乎根本不起作用,但在我的本地主机http://jsfiddle.net/YL6aH/


EDITED: 编辑:

My full PHP loop, together will all the js/html 我完整的PHP循环,将所有的js / html

    <?php 
            $articlesForPreview = $createQuery
                ->query("SELECT * FROM timeline");              
                $fetchAll = $articlesForPreview->fetchAll(PDO::FETCH_ASSOC);    
                foreach($fetchAll as $row) {?>
                <div id='timeline_container'>
                <span class='timeline_date'> <?= $row['time'] ?></span>
                <span class='timeline_title'> <a href='#' onclick=timeline(<?= $row['id'] ?>)><?= $row['title'] ?></a></span>
                <p id='<?= $row['id'] ?>' style='display:none;'> <?= $row['event'] ?></a></span>
            </div>

            <?php }?>



    </aside>
</section>

<script type='text/javascript'>
function timeline(id){
    document.getElementById(id).style.display='inline';
}
</script>

<footer id='footer_container'> 

You should try this : 您应该尝试这样:

function showContent(id){
    $('.active').hide().removeClass('active');
    $('#'+id).show().addClass('active');
}

I see also that you will have multiple elements with id=title , you must change it to make every elem unique. 我还看到您将拥有id=title多个元素, 必须对其进行更改以使每个元素都唯一。

You can simply remember the last item that is visible: 您只需记住可见的最后一项:

var active;
function showContent(id){
    if (active) active.style.display = 'none'; // hide previously visible element
    active = document.getElementById(id);    // keep track of the element you are about to show
    active.style.display='inline'; // show the new element
}

Keep in mind that this solution starts with no items visible and after that only allows one item to be visible at a time. 请记住,此解决方案从没有可见的项目开始,然后只允许一次看到一个项目。

You can go through all elements with an onclick of "showContent", hide them all, afterwards you can just show the one you want. 您可以单击“ showContent”并单击所有元素,将其全部隐藏,然后仅显示所需的元素。

function showContent(id){
    var allElements = document.getElementsByTagName('*');
    for ( var i = 0; i<allElements.length; i++ ) {    
        if ( (allElements[i].onclick + "").indexOf("showContent") >= 0) {
            allElements[i].style.display = "none";       
        }
    }
    document.getElementById(id).style.display='inline';
}

I'm pretty new to javascript and jquery myself, but one of the things we just did in the class I'm taking was the accordion display, where you attach event handlers in the document.ready for the click events for the header objects, and their div children elements, and it was done by swapping the css classes on the click events... are you using css? 我本人对javascript和jquery还是很陌生,但是我们在我正在学习的类中所做的一件事就是手风琴显示,您可以在其中附加事件处理程序到文档中。准备好标题对象的click事件,以及它们的div子元素,这是通过在click事件上交换css类来完成的...您是否使用css? in our version, anytime we clicked on a plus, it would expand the display to display the divs below, and clicking the minus pic it would close them... ours did it for all of them, but you should be able to code that even to "close" all of those displays, and then open/display only the divs that are children for the item clicked... is that what you're looking for? 在我们的版本中,每当我们单击加号时,它将扩大显示范围以显示下面的div,然后单击减号图片将其关闭...我们为所有这些做到了,但是您应该可以对它进行编码甚至“关闭”所有这些显示,然后仅打开/显示作为所单击项目子项的div ...这就是您要查找的内容吗?

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

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