実現したいこと
背景に設定したimgタグで設定した画像の高さをと対象箇所の高さを常に同じになるようにJSで設定したいです。
該当のソースコード
PHP
1foreach($array as $value){ 2 <div> 3 <img id="js-autoHeightBase" src="hoge.png"> 4 <div class="js-autoHeightTarget"></div> 5 </div> 6}
JavaScript
1function auto_config_height() { 2 let base = $("#js-autoHeightBase").height(); 3 console.log(base); 4 let target = document.getElementsByClassName("js-autoHeightTarget"); 5 for(let i = 0; i < target.length; i++){ 6 target[i].style.height = base + 'px'; 7 } 8 9 // $(".js-autoHeightTarget").css('height', base); 10 // $(".js-autoHeightTarget").height(base); 11 // document.getElementsByClassName("js-autoHeightTarget")[0].style.height = base + 'px'; 12 13 // target[0].style.height = base + 'px'; 14 15 16 // debug = $(target).height(); 17 // console.log(target); 18 19 // console.log(debug);tyle.height = wh + "px"; 20}; 21 22$(function() { 23 window.addEventListener("load", auto_config_height, false); 24 window.addEventListener("resize", auto_config_height, false); 25})
試したこと
基準にしてほしい高さはしっかり取得できておりますが、設定することに苦戦しております。
javascriptに加えjqueryを用いて試みておりますが、エラーメッセージも出ません。
回答4件
あなたの回答
tips
プレビュー