如何在jquery中获取当前显示的div id

时间:2016-08-19 11:35:34

标签: javascript jquery

如何在jquery

中获取被阻止的div id

HTML

 <div id="USDC1" style="" class="tablegraphview">USDC2</div>
 <div id="USDC2" style="display:none" class="tablegraphview">USDC2</div>
 <div id="USDC3" style="display:none" class="tablegraphview">USDC3</div>

的Javascript

function tablegraphview(){
var getid = $('.tablegraphview').attr('id');
alert(getid);
}

for ex

<div id="USDC2" style="display:block" class="tablegraphview">USDC2</div>

我需要当前的div id我怎么能在jquery中做到这一点

2 个答案:

答案 0 :(得分:3)

尝试使用$(this).is(':visible')

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="USDC1" style="" class="tablegraphview">USDC2</div>
<div id="USDC2" style="display:none" class="tablegraphview">USDC2</div>
<div id="USDC3" style="display:none" class="tablegraphview">USDC3</div>

<script>
$(function() {

    $(".tablegraphview").each( function(){
        if($(this).is(':visible')){
            alert($(this).attr("id"));
        }
    });

});
</script>

或者

根据@pieter命令

&#13;
&#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div id="USDC1" style="" class="tablegraphview">USDC2</div>
    <div id="USDC2" style="display:none" class="tablegraphview">USDC2</div>
    <div id="USDC3" style="display:none" class="tablegraphview">USDC3</div>
    
    <script>
    $(function() {
    
    	alert($(".tablegraphview:visible").attr('id'));
    
    });
    </script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用此选择器

 $('div').not('[style*="display:none"]')

Working Fiddle