简体   繁体   English

jQuery隐藏div,显示具有新内容的div

[英]jQuery hide div, show div with new content

I am trying to retract my div, then show it with new content based on which link they clicked. 我正在尝试撤消div,然后根据他们单击的链接以新内容显示它。

HTML: HTML:

          <div id="menu">
    <ul>
                <a href="#" title="content_1" alt="about" class="menu"><li>about</li></a>
                <a href="#" title="content_2" alt="contact" class="menu"><li>contact</li></a>
                <a href="#" title="content_3" alt="cv" class="menu"><li>cv</li></a>
            </ul>
        </div>

       <div class="content">
            test
        <div id="content_1" class="content">
            content1
        </div>
        <div id="content_2" class="content">
            content2
        </div>
        <div id="content_3" class="content">
            content3
        </div>
    </div>

JS: JS:

<script type="text/javascript">
        $(document).ready(function(){
            $("a.menu").click(function() {
                var clicked = $(this).attr('title');
                $(".content").hide('slide', {direction: 'right'}, 1000);
                $("#"+clicked).show('slide', {direction: 'left'}, 1000);
            });
        });
    </script>

CSS: CSS:

.content {
position: absolute;

left:303px;
top: 200px;
width: 100%;

margin-top: 200px;
background: #6c7373;

 }

#content_1, #content_2, #content_3 {
display: none;
 }

What happens is: the div retracts, but does not reappear at all, what is going wrong here? 发生了什么:div缩回了,但根本没有出现,这是怎么回事?

Thanks. 谢谢。

First, notice that the container for all of the potential DIVs has the content class so it's being hidden as well. 首先,请注意,所有潜在DIV的容器都有content类,因此它也被隐藏了。 Since the container is hidden, it won't matter if you "show" one of the contained elements. 由于容器是隐藏的,因此“显示”其中包含的元素之一并不重要。 Second, note that the "hide" statement and the "show" statement will have a race condition since they will both apply to the element that's being hidden. 其次,请注意,“ hide”语句和“ show”语句将具有竞争条件,因为它们都将应用于被隐藏的元素。 It would be better to show the item in the callback to the hide operation or exclude it from being hidden. 最好在hide操作的回调中显示该项目或将其从隐藏状态中排除。

<div class="content_wrapper"> <!-- give it a different class -->
        test
    <div id="content_1" class="content">
        content1
    </div>
    <div id="content_2" class="content">
        content2
    </div>
    <div id="content_3" class="content">
        content3
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function(){
        $("a.menu").click(function() {
            var clickedID = '#' + $(this).attr('title');
            $(".content:not(" + clickedID +")").hide('slide', {direction: 'right'}, 1000);
            $(clickedID).show('slide', {direction: 'left'}, 1000);
        });
    });
</script>

将外部.content更改为.content-wrapper

Show and hide are both working at the same time. 显示和隐藏都同时工作。 To avoid the conflict (and not hide then show the content that's visible if the user clicks the same item twice), show the one you want and hide the others by selecting them using siblings() 为避免冲突(如果用户单击两次相同的项目,则不要隐藏然后显示可见的内容),请显示所需的内容,并使用siblings()选择其他内容以隐藏其他内容

Working demo 工作演示

$("a.menu").click(function() {
    var clicked = $(this).attr('title');
    $("#"+clicked).show(1000).siblings().hide(1000);
});

This will also solve the problem you have that you have given the wrapper div the class of .content too. 这也将解决您也给包装div分配.content类的问题。

Also your ul li structure is wrong. 您的ul结构也是错误的。 You need the a tags inside the li. 您需要在li内部使用a标签。 li must come directly after ul. 李必须在ul之后直接来。

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

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