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

jquery中有没有一种方法可以将一个div放在前面?

  •  24
  • anthonypliu  · 技术社区  · 16 年前

    如果我有一堆绝对定位的分割线,它们重叠,我怎么能让一个分割线走到前面?再次感谢各位!

    5 回复  |  直到 12 年前
        1
  •  40
  •   amonett    13 年前

    这是一个css的东西,而不是jquery的东西(尽管您可以使用jquery修改css)。

    $('element').css('z-index', 9999);  // note: it appears 'zIndex' no longer works
    

    或者,绝对定位会把一些东西带到顶端:

    $('element').css('position', 'absolute');
    
        2
  •  24
  •   Yarin    13 年前

    使用jQuery (就像你问的那样):

    $('#a-div').parent().append($('#a-div')); // Pop a div to the front
    

    奇怪的是,每个人都使用z-index。当您只需将其弹出到dom中的前面时,为什么要覆盖自然堆叠顺序?

        3
  •  17
  •   forresto    12 年前

    当使用多个元素时,您必须循环查看哪个元素具有最高的z索引,并将顶部设置为+1。

    http://jsfiddle.net/forresto/Txh7R/

    var topZ = 0;
    $('.class-to-check').each(function(){
      var thisZ = parseInt($(this).css('zIndex'), 10);
      if (thisZ > topZ){
        topZ = thisZ;
      }
    });
    $('.clicked-element').css('zIndex', topZ+1);
    

    对于非css版本(如果没有指定z-index的层),也可以再次附加元素:

    $('.click-to-top').click(function(){
      $(this).parent().append(this);
    });
    

    (我不知道这是否比CSS慢 z-index )

    对于非css非jquery,只需再次附加:

    // var element = document...;
    element.parentNode.appendChild(element);
    
        4
  •  1
  •   airmanx86    16 年前

    使用.css()并应用position属性和z-index属性。

        5
  •  1
  •   Gert Grenander Keiron Lowe    16 年前

    为此使用jquery会有点过分。而是使用css' z-index property :

    <style type="text/css">
      #myElement {
        z-index: 50000;
      }
    </style>