2012-05-13 6 views
1

지금은 z-index이 다른 요소가 있는데 jQuery를 사용하여 그룹화하려고합니다.jQuery를 사용하여 동일한 Z- 인덱스를 갖는 요소를 얻는 방법?

<div class="float" style="z-index:2"></div> 
<div class="float" style="z-index:6"></div> 
<div class="float" style="z-index:10"></div> 
      . 
      . 
      . 
<!-- You get the point --> 

나는 z-index 값을 얻을 수 .attr("style")을 사용할 수 있지만 그렇게하는 것이 좋습니다 될 것 같지 않습니다. (때로는 <style> 태그에도 나타날 수 있습니다.) 그런 것이 있습니까?

var ele = $("*[zindex=5]"); 

앞으로 어떤 해결책이 있을지 기대해보십시오.

$.extend($.expr[':'], { 
    zindex: function(el, i, m) { 
    return $(el).css("z-index") === m[3]; 
    } 
}); 

는 다음과 같이 사용할 수 있습니다 :

$("div:zindex(2)").each(function(){ 
alert(this.innerHTML); 
}); 

를이 기능을 추가하는 경우

+0

그런 다음 일반 JS를 사용하여 정렬'.CSS ('Z- 인덱스')'와 Z- 인덱스를 얻을 수 있습니다. – jimw

+0

@jimw -'$ ("*") .css ('z-index')'는''0 ''을 반환합니다. –

+0

맞다. Z- 인덱스를 얻기 위해서는 각 요소에'.css()'를 적용해야한다. '$ ('*')와 같은 것을 시도해보십시오. 각 (function() {console.log ($ (this) .css ('z-index'))})'. – jimw

답변

2

이있을 것이다 모든 div 요소가 z-index이고이 반환됩니다..

위의 마크 업으로
<div style="position:relative; z-index:2">Foo</div> 
<div style="position:relative; z-index:6">Bar</div> 

, 이전의 jQuery 스크립트 Foo를 알려줍니다.

데모 : http://jsbin.com/icevih/2/edit

+0

이유는 모르지만 작동하지 않습니다. http://jsfiddle.net/DerekL/juDfe/2/ –

+0

'z-index'는 * 배치 된 요소에서만 작동합니다 * : http://jsfiddle.net/ juDfe/3/ – Sampson

+0

죄송합니다. –

0

, 당신은 자신 만의 선택을 만들고 그들에게

$.fn.filterByZIndex = function(zIndex) { 
    var $zElm = $("*").filter(function() { 
     return $(this).css('z-index') == zIndex; 
    }); 
    return $zElm; 
}; 
관련 문제