Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

javascript - Get full height of a clipped DIV

How do I get the height of the div which includes the clipped area of the div ?

<div style="height: 20px; overflow: hidden">
  content<br>content<br>content<br>
  content<br>content<br>content<br>
  content<br>content<br>content<br>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, you cannot do it that way, but it's possible when adding a inner element to your container, like this:

<div id="element" style="height: 20px; overflow: hidden;">
    <p id="innerElement"> <!-- notice this inner element -->
        content<br />content<br />content<br />
        content<br />content<br />content<br />
        content<br />content<br />content<br />
    </p>
</div>

sidenote: wrapping content inside paragraphs is a good practice too, plus that one extra element isn't giving that much of problems, if any at all...

And JavaScript:

var innerHeight = document.getElementById('innerElement').offsetHeight;
alert(innerHeight);

P.S. For this JavaScript to work, put it after your #element div, because plain JavaScript is executed before DOM is ready if it's not instructed to do so. To make this work when DOM is ready, check this.

But I'd suggest getting jQuery, it will come in handy later on if you're going to extend JavaScript operations in your site.

Plus, jQuery is the power, for real!

That way, simply add this script to your <head /> (assuming you've jQuery included):

$(document).ready(function() {
 var innerHeight = $('#innerElement').height();
 alert(innerHeight);
});

Example @jsFiddle using jQuery way!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...