掲示を表示する PHP ページがあり、テキストに画像が埋め込まれています。ここに簡略化されたコードを追加しました。問題は、画像のサイズを変更しようとすると、DOM が画像を参照できないことです。実際のファイルでは、ページを新たにロードすると画像を参照できませんが、(日付セレクターを使用して) そのページに戻ると、突然 Javascript のサイズ変更が正常に機能します。画像が初めてロードされたときに機能しません。
function resizeImg () {
var w=document.getElementById("textImg").width;
var h=document.getElementById("textImg").height;
alert(h+" "+w);
if (w>300 || h>300) {
if (w>300) {
var factor=((300/w));
} else {
if (h>300) {
var factor=((300/h));
}
}
w*=factor;w=Math.round(w);
h*=factor;h=Math.round(h);
document.getElementById("textImg").style.width=w+"px";
document.getElementById("textImg").style.height=h+"px";
}
}//end FUNCTION
ヘッダ:
<?php
$pic="20130213.gif";
$blText="Yes, you heard right. Thats all we are going to have for dinner. Why? because cream of corn is good when you put sugar in it, with some pepper and butter. So, quit your complaining and eat the slop.";
?>
HTML:
<body>
<div id="bullText" class="tBorder">
<div class="tBorder" id="bullTextArea">
<?php
$file="../../Admin/aPages/upload/".$pic;
echo "<img id='textImg' class='textImage' src='".$file."' alt='no image' />";
echo $blText
?>
</div>
</div><!--BULL TEXT DIV-->
<?php
echo "<script>resizeImg ()</script>";
?>
</body>