简体   繁体   English

在jQuery中显示另一个div时隐藏一个div?

[英]hide one div when another is showing in jQuery?

I am trying to hide a div when another one is visible. 当另一个可见时,我试图隐藏一个div。

I have div 1 and div 2. 我有div 1和div 2。

If div 2 is showing then div 1 should hide and if div 2 is not showing then div 1 should be visible/unhide. 如果显示div 2,则div 1应该隐藏;如果未显示div 2,则div 1应该可见/取消隐藏。

The function would need to be function/document ready upon page load. 该功能需要在页面加载时准备好功能/文档。

I've tried this but I'm not having any luck, can someone please show me how I can do this. 我已经尝试过了,但是没有运气,有人可以告诉我如何做到这一点。

<script>
    window.onLoad(function () {
        if ($('.div2').is(":visible")) {
             $(".div1").fadeOut(fast);

        } else if ($('.div2').is(":hidden")) {
            $('.div1').fadeIn(fast);

        }
    });
</script>

Add a class of hidden to each div, then toggle between that class using jQuery. 向每个div添加一个hidden类,然后使用jQuery在该类之间切换。 By the way, window.onload is not a function, it expects a string like window.onload = function() {} . 顺便说一句, window.onload不是一个函数,它需要像window.onload = function() {}这样的字符串。 Also, put fast in quotations. 另外,请fast报价。 I don't know if that's required , but that's how jQuery says to do it. 我不知道这是否是必需的 ,但是jQuery就是这么说的。

<div class="div1"></div>
<div class="div2 hidden"></div>

.hidden { display: none }

$(document).ready(function() {

    if($(".div1").hasClass("hidden")) {
        $(".div2").fadeIn("fast");
    }

    else if($(".div2").hasClass("hidden")) {
        $(".div1").fadeIn("fast");
    }

});

You should pass a string to the .fadeIn() and .fadeOut() methods. 您应该将字符串传递给.fadeIn().fadeOut()方法。

Instead of .fadeIn(fast) it'll be .fadeIn("fast") . 而不是.fadeIn(fast)而是.fadeIn("fast") Same for .fadeOut() . .fadeOut()相同。

And in general since you're already using jQuery it's better to wrap your code like this: 通常,由于您已经在使用jQuery,因此最好像这样包装代码:

$(function () {
    // Code goes here
});

You can use setTimeout or setInterval to track if these divs exists 您可以使用setTimeout或setInterval来跟踪这些div是否存在

$(function() {
  var interval = window.setInterval(function() {
    if($('#div2').hasClass('showing')) {
      $('#div1').fadeOut('fast');
    }      
    if($('#div2').hasClass('hidden')) {
      $('#div1').fadeIn('fast');
    }
  }, 100);

  // when some time u don't want to track it
  // window.clearInterval(interval)
})

for better performance 以获得更好的性能

var div1 = $('#div1')
  , div2 = $('#div2')


var interval ....
// same as pre code

It looks like you're using jquery selectors (a javascript library). 看来您正在使用jquery选择器(一个JavaScript库)。 If you're going to use jquery make sure the library is loaded properly by including it in the document header (google makes this easy by hosting it for you 如果您要使用jquery,请通过将其包含在文档标头中来确保正确加载了该库(google通过为您托管它来简化此操作)
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> ) <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

With jQuery loaded you can do it like this 加载jQuery后,您可以像这样操作

$(document).ready(function(){
if ($('.div1').is(":visible")) {
$('div2').hide();
}

else if ($('.div2').is(":visible")) {
$('div1').hide();
}

});

WORKING EXAMPLE : http://jsfiddle.net/HVDHC/ - just change display:none from div 2 to div 1 and click 'run' to see it alternate. 工作示例http : //jsfiddle.net/HVDHC/-只需将display:none从div 2更改为div 1,然后单击“运行”以查看它是否备用。

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

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