2016-05-23 67 views
0

我想在发送到打印机之前找到一种方法来发送自己的警报,如果任何div大于预设大小。获取页面上所有div ID的高度

每个页面将有所不同,并有不同的样本测试问题的div ID。

此外,每个div的高度不会像预先设置的那样高。 目前,我有这个,但它不工作,我会希望:

<style> 
#one{background:#ddd;height:250px;} 
#two{background:#000;height:300px;} 
</style> 
<div id="page"> 
    <div id="one"> 
    stuff 
    </div> 
    <div id="two"> 
    more stuff 
    </div> 
</div> 
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> 
<script> 
    var IDs = $("#page div[id]")   
    .map(function() { return this.id; }) 
    var result = $(this.id).height(); 
    if(result > 275){ 
    alert("over"); 
    }else{ 
    alert("under"); 
    } 
</script> 

我确实觉得我靠近。任何帮助是极大的赞赏。

+1

为什么你得到一组ID,然后从来没有使用它?也许你应该看看['each'](https://api.jquery.com/each/)方法。 –

回答

1

您并不需要id s,但如果您想将搜索范围限制为id s,则表示您已正确完成此操作。当您尝试使用结果时,问题会随之而来。

您希望遍历一组匹配元素。 jQuery提供each做到这一点:

$("#page div[id]").each(function() { 
    // This is called once for each matching div; 
    // `this` refers to each of the divs. So: 
    if ($(this).height() > 275) { 
     alert(this.id " is over"); 
    } else { 
     alert(this.id + " is under"); 
    } 
}); 
+1

谢谢。我必须稍微更改一下代码才能使其工作,但它的确行得通。 –