【问题标题】:Jquery show/hide div element depending certain page element is loadedJquery 显示/隐藏 div 元素,具体取决于加载的某些页面元素
【发布时间】:2012-09-09 08:23:58
【问题描述】:

我想让jquery检测当前页面是否加载了某个div id然后隐藏另一个div id元素,例如:

<div id="A1">some content here</div> //initially this div is visible
<div id="C1" style="display: none;">some content here</div> //initially this div is invisible
<div id="B1">some content here</div>

如果 Jquery 检测到 #B1 已加载到当前页面,则隐藏 #A1 并显示 #C1。

非常感谢

【问题讨论】:

    标签: jquery show-hide


    【解决方案1】:
    if ($("#B1").length > 0) {
      $("#A1").hide();
      $("#C1").show();
    }
    

    【讨论】:

      【解决方案2】:
      $(function () {
          if ($('#B1').length) {
              $('#A1').hide();
              $('#C1').show();
          }
      });
      

      【讨论】:

        【解决方案3】:

        我假设您在加载文档后加载 div, 所以,

        $('#B1').load('ajax/test.html', function() {
          $('#A1').hide();
          $('#C1').show();
        
        }); 
        

        如果您不使用 jQuery,这将在 #B1 元素上实现 onreadystatechanged。 其他明智的做法是准备好文件, $(函数(){
        $('#A1").hide(); $("#C1").show();});

        【讨论】:

          最近更新 更多