简体   繁体   English

如何计算溢出y隐藏类

[英]How to count overflow-y hidden class

In jquery, how to count overflow-y hidden div class ? 在jQuery中,如何计算溢出y隐藏的div类? I tried to used this but it only count numbers of hide() 我试图使用它,但它只计算hide()

$(".classname > *").filter(":hidden").size();

How can I count in overflow-y: hidden ? 我怎么算overflow-y: hidden

You can try something like this: 您可以尝试如下操作:

var length = $(".classname > *").filter(function() {
  return this.style.overflowY == "hidden"
}).size();
alert(length);

The thing is :hidden is a selector which is used to check if any dom node is not visible in the screen. 问题是:hidden是一个选择器,用于检查屏幕上是否看不到任何dom节点。

But you want to see how many elements in a particular object has the css property of overflow-y:hidden; 但是,您想查看一个特定对象中有多少个元素具有css属性的overflow-y:hidden; .

So this is a css property so you have two ways: 因此,这是一个CSS属性,因此您有两种方法:

in javascript use .style.overflowY : 在javascript中使用.style.overflowY

elem.style.overflowY == "hidden"

and using jquery: 并使用jQuery:

$(this).css('overflow-y') == "hidden"

 var length = $(".clas > *").filter(function() { return this.style.overflowY == "hidden" }).size(); alert(length); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='clas'> <p style='overflow-y:hidden;'></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p style='overflow-y:hidden;'></p> </div> 

Suppose you have the following html mark-up 假设您具有以下html标记

<style>
    .className
    {
        overflow-y:hidden;
    }
</style>
<div class="className">div1</div>
<div class="className">div2</div>
<div class="className" style="overflow-y:auto">div3</div>
<div class="className" style="overflow-y:auto">div4</div>
<div class="className">div5</div>
<div class="className">div6</div>

Then your jQuery code should look something like this 然后您的jQuery代码应该看起来像这样

$(document).ready(function()
{
    alert($('div.className').filter(function() {
    return $(this).css('overflow-y') == 'hidden';
    }).size());
});

If you have the following mark-up without any class specification 如果您有以下标记,但没有任何类别说明

<div style="overflow-y:hidden">div1</div>
<div style="overflow-y:hidden">div2</div>
<div style="overflow-y:auto">div3</div>
<div style="overflow-y:auto">div4</div>
<div style="overflow-y:hidden">div5</div>
<div style="overflow-y:hidden">div6</div>

Simply remove .className from $('div.className') selector. 只需从$('div.className')选择器中删除.className And your code is as follows 和你的代码如下

$(document).ready(function()
{
    alert($('div').filter(function() {
    return $(this).css('overflow-y') == 'hidden';
    }).size());
});

EDIT 编辑

The above approach applies to parent element only. 以上方法仅适用于父元素。 If you need a selector for children only , simply use 如果需要儿童选择器,则只需使用

$(document).ready(function()
{
    alert($('div.className > *').filter(function() {
    return $(this).css('overflow-y') == 'hidden';
    }).size());
});

If you need a selector for the parent and children , you can use something like 如果您需要父母和孩子的选择器,则可以使用类似

$(document).ready(function()
{
    alert($('div.className, div.className *').filter(function() {
    return $(this).css('overflow-y') == 'hidden';
    }).size());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM