简体   繁体   English

fadeOut()仅淡出第一个元素

[英]fadeOut() only fades out the first element

By default, I have several DIVs hidden and then I fade them in when the user clicks on a certain button. 默认情况下,我隐藏了几个DIV,然后在用户单击某个按钮时淡入它们。 That works fine but when I try to close a .holder DIV using a span within said .holder DIV, only the first one works. 效果很好,但是当我尝试使用上述.holder DIV中的跨度关闭.holder DIV时,只有第一个起作用。 When I click the others, nothing happens. 当我单击其他按钮时,没有任何反应。 I get no error or any sort of visual feedback whatsoever. 我没有任何错误或任何形式的视觉反馈。

The markup: 标记:

<div class="holder" id="window_one">
    <div class="title_bar">
        <p>Window 1</p>
        <div class="control_holder">
            <span class="controls" id="close">X</span>
            <span class="controls" id="minimize">_</span>
        </div>
    </div>
    <div class="interface">
        <p>Testing123</p>
    </div>
</div>
<div class="calculator" id="window_two">
    <div class="title_bar">
        <p>Window 2</p>
        <div class="control_holder">
            <span class="controls" id="close">X</span>
            <span class="controls" id="minimize">_</span>
        </div>
    </div>
    <div class="interface">
        <p>Testing123</p>
    </div>
</div>

The jQuery: jQuery:

$(document).ready(function() {
    $('#close').click(function() {
        $(this).parents('.holder').fadeOut(250);
    });
});

What exactly am I doing wrong here? 我到底在做什么错? I'm using jQuery 1.10.2 if that makes any difference. 我正在使用jQuery 1.10.2,如果有什么区别。

I'd demo the code on jsFiddle but is seems to be down atm. 我演示了jsFiddle上的代码,但似乎在atm下。

You can not have the same id of two element on the page. 您在页面上不能具有两个元素的相同ID。 If you want to do that give it as a class name like - 如果您要这样做,可以将其命名为类名-

<div class="holder" id="window_one">
    <div class="title_bar">
        <p>Window 1</p>
        <div class="control_holder">
            <span class="controls close">X</span>
        </div>
    </div>
    <div class="interface">
        <p>Testing123</p>
    </div>
</div>
<div class="calculator" id="window_two">
    <div class="title_bar">
        <p>Window 2</p>
        <div class="control_holder">
            <span class="controls close">X</span>
        </div>
    </div>
    <div class="interface">
        <p>Testing123</p>
    </div>
</div>

and the Jquery like - 和Jquery一样-

$(document).ready(function() {
    $('.close').click(function() {
        $(this).parents('.holder').fadeOut(250);
    });
});

Hope this will help. 希望这会有所帮助。

Here is how it should be: 应该是这样的:

<div class="holder" id="window_one">
    <div class="title_bar">
        <p>Window 1</p>
        <div class="control_holder">
            <span class="controls close">X</span>
        </div>
    </div>
    <div class="interface">
        <p>Testing123</p>
    </div>
</div>
<div class="calculator" id="window_two">
    <div class="title_bar">
        <p>Window 2</p>
        <div class="control_holder">
            <span class="controls close">X</span>
        </div>
    </div>
    <div class="interface">
        <p>Testing123</p>
    </div>
</div>

and the JavaScript: 和JavaScript:

$(document).ready(function() {
    $('.close').click(function(e) {

        $(this).parents('.holder').forEach(function(){

                  $(this).fadeOut(250);

                      });

         e.preventDefault();
    });

});

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

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