隐藏div javascript

时间:2014-01-22 19:47:53

标签: javascript html css

我目前在我的页面上有两个div,一个叫做结果,一个叫我的网站上的投资组合。我有一些javascript在两者之间切换。目前,组合div加载,但我想切换订单。起初,这看起来很简单,我只想切换

    <div style="display:none">

到结果部分,然而这会抛弃我的页面。这是所有的html标记。

        <script type="text/javascript">
            function swap(one, two) {
            document.getElementById(one).style.display = 'block';
            document.getElementById(two).style.display = 'none';
        }
        </script>

                <span id="two">
                    <header class="title one">Portfolio</header>
                    <div class="spacer"></div>
                    <div class="title two"><?php echo fuel_var('body', 'This is a default layout. To change this layout go to the fuel/application/views/_layouts/main.php file.'); ?></div>
                    <section class="featured-slider">
                        <div id="ca-container" class="ca-container">
                            <div class="nav-featured">
                                <div class="prev-featured"></div>

                                <div class="next-featured"></div>
                            </div>
                            <div class="main-carousel hideme dontHide">
                                <div class="ca-wrapper portfolio_wrapper">
                                    <div class="ca-item ca-item-1">
                                        <div class="f-single">
                                            <a data-gal="prettyPhoto[gallery1]" href="<?php echo base_url()?>assets/images/screenshots/AW_Portfolio_ARDL.png">
                                                <div class="f-image">
                                                    <img src="<?php echo base_url()?>assets/images/Clevelandmetroparks2.jpg" alt='img'>
                                                    <div class="image-hover-overlay"></div>
                                                    <span class="f-category"></span>
                                                    <div class="portfolio-meta">
                                                        <div>ARDL</div>
                                                        <div class="clear"></div>
                                                        <div>Database Screenshot</div>
                                                    </div>
                                                </div>
                                                <div class="f-info">ARDL</div>
                                            </a>
                                        </div>
                                    </div>
                                    <div class="ca-item ca-item-2">
                                        <div class="f-single">
                                            <a data-gal="prettyPhoto[gallery1]" href="<?php echo base_url()?>assets/images/screenshots/AW_Portfolio_Polymerics.png">
                                                <div class="f-image">
                                                    <img src="<?php echo base_url()?>assets/images/Archive/Polymerics" alt='img'>
                                                    <div class="image-hover-overlay"></div>
                                                    <span class="f-category"></span>
                                                    <div class="portfolio-meta">
                                                       <div>Polymerics</div>
                                                        <div class="clear"></div>
                                                        <div>Database Screenshot</div>
                                                    </div>
                                                </div>
                                                <div class="f-info">Polymerics</div>
                                            </a>
                                        </div>
                                    </div>
                                   <div class="ca-item ca-item-3">
                                        <div class="f-single">
                                            <a data-gal="prettyPhoto[gallery1]" href="<?php echo base_url()?>assets/images/screenshots/AW_Portfolio_Raune.png">
                                                <div class="f-image">
                                                    <img src="<?php echo base_url()?>assets/images/Archive/Ru.jpg" alt='img'>
                                                    <div class="image-hover-overlay"></div>
                                                    <span class="f-category"></span>
                                                    <div class="portfolio-meta">
                                                        <div>Ruane Attorneys</div>
                                                        <div class="clear"></div>
                                                        <div>Database Screenshot</div>
                                                    </div>
                                                </div>
                                                <div class="f-info">Ruane Attorneys</div>
                                            </a>
                                        </div>
                                    </div>
        <!--
                                    <div class="ca-item ca-item-4">
                                        <div class="f-single">
                                            <a data-gal="prettyPhoto[gallery1]" href="img/screenshots/AW_results_connecticut.png">
                                                <div class="f-image">
                                                    <img src="img/featured/feat-04.jpg" alt='img'>
                                                    <div class="image-hover-overlay"></div>
                                                    <span class="f-category"></span>
                                                    <div class="portfolio-meta">
                                                        <div>Connecticut Public Defenders</div>
                                                        <div class="clear"></div>
                                                        <div>Database Screenshot</div>
                                                    </div>
                                                </div>
                                                <div class="f-info">Connecticut Public Defenders</div>
                                            </a>
                                        </div>
                                    </div>
        -->
                                    <div class="ca-item ca-item-5">
                                        <div class="f-single">
                                            <a data-gal="prettyPhoto[gallery1]" href="<?php echo base_url()?>images/screenshots/AW_results_ clevMetroparks.png">
                                                <div class="f-image">
                                                    <img src="<?php echo base_url()?>assets/images/Archive/Clevelandmetroparks.jpg" alt='img'>
                                                    <div class="image-hover-overlay"></div>
                                                    <span class="f-category"></span>
                                                    <div class="portfolio-meta">
                                                        <div>Cleveland Metroparks</div>
                                                        <div class="clear"></div>
                                                        <div>Database Screenshot</div>
                                                    </div>
                                                </div>
                                                <div class="f-info">Cleveland Metroparks</div>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                      </div>
                    </section>
                    <div>
                    </div>
                    </span>
                    <div style="display:none" id="one">
                    <header class="title one">Results</header>
                    <div class="spacer"></div><br><br>
                    <section class="featured-slider">
                        <div id="ca-container" class="ca-container">
                            <div class="main-carousel hideme dontHide">
                                <div class="ca-wrapper results_wrapper">
                                         <div class="ca-item ca-item-1">
                                        <div class="f-single_two">
                                            <a href="https://vimeo.com/84619379" class="mfp-iframe" title="My YouTube Video">
                                                <div class="f-image">
                                                    <img src="<?php echo base_url()?>assets/images/reslults_shit/cle2.jpg" alt='img'>
                                                    <div class="image-hover-overlay"></div>
                                                    <span class="f-category"></span>
                                                    <div class="portfolio-meta">
                                                        <div>Cleveland Metroparks</div>
                                                        <div class="clear"></div>
                                                        <div>Database Video</div>
                                                    </div>
                                                </div>
                                                <div class="f-info">Cleveland Metroparks</div>
                                            </a>
                                        </div>
                                    </div>
                                  <div class="ca-item ca-item-2">
                                        <div class="f-single_two">
                                            <a href="http://vimeo.com/84619331" class="mfp-iframe" title="My YouTube Video">
                                                <div class="f-image">
                                                    <img src="<?php echo base_url()?>assets/images/reslults_shit/connecticut2.jpg" alt='img'>
                                                    <div class="image-hover-overlay"></div>
                                                    <span class="f-category"></span>
                                                    <div class="portfolio-meta">
                                                        <div>Connecticut Public Defenders</div>
                                                        <div class="clear"></div>
                                                        <div>Database Video</div>
                                                    </div>
                                                </div>
                                                <div class="f-info">Connecticut Public Defenders</div>
                                            </a>
                                        </div>
                                    </div>
                                    <div class="ca-item ca-item-3">
                                        <div class="f-single_two">
                                            <a data-gal="prettyPhoto[gallery1]" href="<?php echo base_url()?>assets/images/Screenshots/AW_Portfolio_ARDL.png">
                                                <div class="f-image">
                                                    <img src="<?php echo base_url()?>assets/images/reslults_shit/ardl2.jpg" alt='img'>
                                                    <div class="image-hover-overlay"></div>
                                                    <span class="f-category"></span>
                                                    <div class="portfolio-meta">
                                                        <div>ARDL</div>
                                                        <div class="clear"></div>
                                                        <div>Database Screenshot</div>
                                                    </div>
                                                </div>
                                                <div class="f-info">ARDL</div>
                                            </a>
                                        </div>
                                    </div>






                </article>

3 个答案:

答案 0 :(得分:3)

如果您熟悉jQuery,那么可以采用更简单的方法。

$('#divName').hide();
$('#divName').show();

将它们放在你的切换/交换功能中。

P.S。:如果您使用的是id(即:div id =“divName”),则前缀为'#',如果您使用的是类,则在前面添加'。'。您还可以使用$('#divName').css('display', 'none')$('#divName').css('display', 'block')

答案 1 :(得分:1)

以下JavaScript应该有效:

<script>
function swap(one, two) {
    document.getElementById(one).style.display = 'block';
    document.getElementById(two).style.display = 'none';
}
</script>
<div id="first" style="display:none"><p>One</p></div>
<div id="second"><p>Two</p></div>

<button onclick="swap('first','second')">Swap</button>

答案 2 :(得分:0)

我找到了一个解决方案:当我把它显示为一个块时,我想到了一些东西,所以我把我的javascript更改为:

<script type="text/javascript">
    function swap(one, two) {
    document.getElementById(one).style.height = '100%';
    document.getElementById(one).style.width = '100%';

    document.getElementById(two).style.height = '0';
    document.getElementById(two).style.width = '0';
}
</script>

希望这有助于其他任何人。