私はJavaScriptが初めてで、練習しています。「test」というクラス名を持つ div 内にある h1 要素と p 要素の高さを合わせて表示しようとしています。私はあらゆる種類のものを調べましたが、私は本当に近いように感じますが、これをうまく機能させることはできません. 誰でも助けることができますか?
HTML:
<div class="test">
<h1>Understanding Scope</h1>
<p>By understanding code's <em>scope</em>, we know when that code affects only one part of our code, or the entire codebase. If we create things that are <em>global</em> in scope, we are giving any code the supreme power to control our code. So we want to protect our code by being very careful when creating things that are global in scope. This is especially important if you plan on using JavaScript libraries like jQuery.</p>
</div>
<h1>Local Scope</h1>
<p>JavaScript uses <em>function scope</em>, meaning every time we create a new function the scope changes. Any code inside that function is <em>local</em> to that function. Code that is local in scope is not accessible to outside code.</p>
JavaScript:
function testing(){
if (document.getElementsByClass('test')){
var headerHeight = document.getElementsByTagName('h1').offsetHeight;
var textHeight = document.getElementsByTagName('p').offsetHeight;
var totalHeight = headerHeight + textHeight;
alert(totalHeight);
}
}
testing();