我有一个显示公告的 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>