简体   繁体   English

多个切换几乎就像Tabs JavaScript / CSS

[英]Multiple Toggles Almost like Tabs JavaScript/ CSS

I want to create tabs that function like toggles, but instead only one toggle can be active at one time. 我想创建功能类似于切换的标签,但一次只能激活一个切换。 The content of the tab also needs to be above the the tabs themselves. 选项卡的内容也需要在选项卡本身上方。 The problem is I am using a loop to generate the content as well as the tabs themselves. 问题是我正在使用循环来生成内容以及选项卡本身。

I've got a nice javascript code right here that works perfectly fine. 我在这里有一个不错的javascript代码,可以很好地工作。 I understand this perfectly as well. 我也很了解这一点。 The only problem is that it obviously doesn't disable other toggles when another one is clicked. 唯一的问题是,当单击另一个开关时,它显然不会禁用其他开关。 Also having one "tab" / "toggle" always active. 同时具有一个“选项卡” /“切换”始终处于活动状态。 Code that could probably check the div id with a prefix of "post" and make all div id's with "post" to display:none besides the one that was clicked on. 该代码可能可以检查前缀为“ post”的div id,并使所有带有“ post”的div id都显示:除了被单击的div id之外,没有其他代码。 That would be perfect if that could be possible. 如果可能的话,那将是完美的。 Another way I could think of this is putting all the generated content into a container, and it simply disables all id's in the container besides the one that was clicked. 我可以想到的另一种方式是将所有生成的内容放入一个容器中,它只是禁用了该容器中除单击的ID外的所有ID。

If this code can be modified to achieve that, it would be great. 如果可以修改此代码来实现这一点,那就太好了。 This is some very simple code that I understand very clearly. 这是一些非常简单的代码,我非常清楚地了解。 All I need is that it behaves more like tabs where only one can be active at once. 我需要的是,它的行为更像选项卡,其中一次只能激活一个选项卡。

<script type="text/javascript">
function toggleMeMirror(a){
    var e=document.getElementById(a);
    if(!e) return true;
    if(e.style.display=="none"){
        e.style.display="inline"
    } else {
        e.style.display="none"
    }
    return true;
}
</script>

HTML / PHP HTML / PHP

Loop 1 - The Content 循环1-内容

<?php while ($queryepisodemirrors->have_posts()) : $queryepisodemirrors->the_post(); ?>
        <?php if(get_post_meta(get_the_ID(), 'parentIDmirror', true) == $postIDCheck) { ?>

        <div id="post-<?php the_ID(); ?>" style="display:none;">
                    <center><?php the_title(); ?><br /><br />
                    <?php echo get_post_meta(get_the_ID(), 'mirror_embed_code', true); ?>
                    <?php wprp(true);?>
                </center>
        </div>  
        <?php } ?>
<?php endwhile; ?>

Loop 2 - The actual toggles / tabs 循环2-实际的切换/制表符

<?php while ($queryepisodemirrors->have_posts()) : $queryepisodemirrors->the_post(); ?>
        <?php if(get_post_meta(get_the_ID(), 'parentIDmirror', true) == $postIDCheck) { ?>
        <div style="float: left; padding: 4px;">
        <center>



        <div class="post-<?php the_ID(); ?>" onclick="return toggleMeMirror('post-<?php the_ID(); ?>')" >
            <div style="overflow: hidden; width: 150px; height: 100px;">
                <div style="background: rgb(0, 0, 0) transparent; /* fallback color */ background: rgba(0, 0, 0, 0.8); color: white; padding: 2px;">
                    <center>

                        <?php echo get_post_meta(get_the_ID(), 'video_provider', true);
                        echo ' Mirror';?>
                    </center>
                </div>
                <img src="<?php echo $thumbnail_src; ?>" width="150px"/>

            </div>
        </div>


        </center>
        </div>

        <?php } ?>
<?php endwhile; ?>

This is also tagged jquery so I'll just recommend that you include the jquery library and include: 这也被标记为jquery,所以我建议您包括jquery库并包括:

$('.someclass').hide();

as the first line in the toggleMeMirror function. 作为toggleMeMirror函数的第一行。

Then, make sure that each of your looped content divs exist in the class "someclass". 然后,确保每个循环的内容div位于“ someclass”类中。

like: 喜欢:

foreach($this as $that) {
    print "<div class='someclass'>$that</div>";
}

then 然后

function toggleMeMirror(a){
    // hide all
    $('.someclass').hide();
    //  show one
    var e=document.getElementById(a);
    if(!e) return true;
        if(e.style.display=="none"){
            e.style.display="inline"
        } else {
            e.style.display="none"
        }
    return true;
}

You can try something like this DEMO 您可以尝试这样的演示

HTML Code HTML代码

<div id="one"><img src='http://img.widgetbox.com/thumbs/302/8fb0669c-72ee-454d-a08f-9700b8a5270a.png?4' height='50px' widht='50px' ></div><br>
<div id="two"><img src='http://ww2.valdosta.edu/~mecarter/Spongebob%20Reading.png' height='50px' widht='50px' ></div><br>
<div id="three"><img src='http://www.cliffdweller.com/blogPhotos/wrappingpaperbase/Spongebob%20Waving.png' height='50px' widht='50px' ></div><br><br>
<div id="thumb"><img src='http://img.widgetbox.com/thumbs/302/8fb0669c-72ee-454d-a08f-9700b8a5270a.png?4' height='200px' widht='200px' ></div>

jQuery jQuery的

$("#one").click(function() {
  $("#thumb").html("<img src='http://img.widgetbox.com/thumbs/302/8fb0669c-72ee-454d-a08f-9700b8a5270a.png?4' height='200px' widht='200px' >");
});

$("#two").click(function() {
  $("#thumb").html("<img src='http://ww2.valdosta.edu/~mecarter/Spongebob%20Reading.png' height='200px' widht='200px' >");
 });

$("#three").click(function() {
   $("#thumb").html("<img src='http://www.cliffdweller.com/blogPhotos/wrappingpaperbase/Spongebob%20Waving.png' height='200px' widht='200px' >");
});

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

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