代码之家  ›  专栏  ›  技术社区  ›  Srini

jQuery选择器父母和子女?

  •  0
  • Srini  · 技术社区  · 15 年前

    我想要一个类似于Wordpress对posts-actions所做的事情。

    我的HTML

    <div class="onhover">
      <p class="">This is a paragraph. My background will be yellow when you hover over me — even if you're using Internet Explorer.</p>
      <div class="actions"><a href="#">Add</a> | <a href="#">Edit</a> | <a href="#">Modify</a> | <a href="#">Delete</a></div>  
    </div>
    <div class="onhover">  
      <p class="">Move your mouse over me! Move your mouse over me! I'll turn yellow, too! Internet Explorer users welcome!</p>
      <div class="actions"><a href="#">Add</a> | <a href="#">Edit</a> | <a href="#">Modify</a> | <a href="#">Delete</a></div>
    </div>
    

    * { margin:0; padding:0; font-family:Verdana, Arial, Helvetica, sans-serif; }
        div.actions { padding:5px; font-size:11px; visibility: hidden; }
        #hover-demo1 p:hover {
            background: #ff0;
        }
        .pretty-hover {
            background: #fee;
            cursor: pointer;
        }
    

    滑动分页

    $(document).ready(function() {
          $('.onhover p').hover(function() {
            $(this).addClass('pretty-hover');
            $('div.actions').css('visibility','visible');
          }, function() {
            $(this).removeClass('pretty-hover');
            $('div.actions').css('visibility','hidden');
          });
        });
    

    我想要的是,在悬停一个特定的P元素时,相应的操作应该是可见的,当前在hover一个P元素上所有其他的操作都是可见的。我该如何局限于某个特定的?

    2 回复  |  直到 15 年前
        1
  •  2
  •   cletus    15 年前

    不需要设置CSS可见性属性。已经有一些jQuery方法可以隐藏和显示内容。只要使用 next() 遍历法。

    $(document).ready(function() {
      $('.onhover p').hover(function() {
        $(this).addClass('pretty-hover');
        $(this).next().show();
      }, function() {
        $(this).removeClass('pretty-hover');
        $(this).next().hide();
      });
    });
    
        2
  •  0
  •   stefita    15 年前

    $(document).ready(function() {
      $('div.actions').hide();
      $('.onhover p').hover(function() {
        $(this).addClass('pretty-hover');
        $(this).children('div.actions:first').show();
      }, function() {
        $(this).removeClass('pretty-hover');
        $(this).children('div.actions:first').hide();
      });
    });