Javascript 在chrome中无法正常工作的远视

Javascript 在chrome中无法正常工作的远视,javascript,google-chrome,height,Javascript,Google Chrome,Height,我遇到了一个有趣的问题。我不知道是什么原因。我试图在网上搜索,但没有找到解决办法。问题是offsetWidth在Chrome中工作得很好,但offsetWidth不能正常工作。有时返回值,但有时不返回。请问有没有解决这个问题的办法 注意:我尝试使用id.height、id.clientHeight。但所有人都给了我同样的问题。在我的项目中,我们还使用Prototype和JQuery 我还使用了原型getHeight()。。但不在Chrome中工作。getWidth()工作正常。但所有这些都在Fi

我遇到了一个有趣的问题。我不知道是什么原因。我试图在网上搜索,但没有找到解决办法。问题是offsetWidth在Chrome中工作得很好,但offsetWidth不能正常工作。有时返回值,但有时不返回。请问有没有解决这个问题的办法

注意:我尝试使用id.height、id.clientHeight。但所有人都给了我同样的问题。在我的项目中,我们还使用Prototype和JQuery

我还使用了原型getHeight()。。但不在Chrome中工作。getWidth()工作正常。但所有这些都在Firefox中运行良好…:(

这是代码块

$('leftImage_<?php echo $iExtLoop; ?>').style.position="relative";
$('leftImage_<?php echo $iExtLoop; ?>').style.left= ($('leftImageDiv').offsetWidth - $('leftImage_<?php echo $iExtLoop; ?>').offsetWidth)/2 + "px";
$('leftImage_<?php echo $iExtLoop; ?>').style.top= ($('leftImageDiv').offsetHeight - $('leftImage_<?php echo $iExtLoop; ?>').offsetHeight)/2 + "px";

if($('stripLeft_<?php echo $iExtLoop; ?>')){
    $('stripLeft_<?php echo $iExtLoop; ?>').style.position="relative";
    $('stripLeft_<?php echo $iExtLoop; ?>').style.left= ($('leftImageDiv').offsetWidth - $('stripLeft_<?php echo $iExtLoop; ?>').offsetWidth) + "px";
    $('stripLeft_<?php echo $iExtLoop; ?>').style.top= ($('leftImageDiv').offsetHeight - $('stripLeft_<?php echo $iExtLoop; ?>').offsetHeight) + "px";                                      
}
$('leftImage').style.position=“relative”;
$('leftImageDiv').style.left=($('leftImageDiv').offsetWidth-$('leftImageDiv').offsetWidth)/2+“px”;
$('leftImage').style.top=($('leftImageDiv').offsetHeight-$('leftImage').offsetHeight)/2+“px”;
如果($('stripLeft')){
$('stripLeft').style.position=“relative”;
$('stripLeft').style.left=($('leftImageDiv').offsetWidth-$('stripLeft').offsetWidth)+“px”;
$('stripLeft').style.top=($('leftImageDiv').offsetHeight-$('stripLeft').offsetHeight)+“px”;
}

重构代码,由西姆·维达斯添加:

var img = $( 'leftImage_<?php echo $iExtLoop; ?>' );
var div = $( 'leftImageDiv' );
var strip = $( 'stripLeft_<?php echo $iExtLoop; ?>' );

img.style.position = "relative";
img.style.left = ( div.offsetWidth - img.offsetWidth ) / 2 + "px";
img.style.top = ( div.offsetHeight - img.offsetHeight ) / 2 + "px";

if ( strip ) {
    strip.style.position = "relative";
    strip.style.left = ( div.offsetWidth - strip.offsetWidth ) + "px";
    strip.style.top= ( div.offsetHeight - strip.offsetHeight ) + "px";
}
var img=$('leftImage_');
var div=$('leftImageDiv');
var strip=$('stripLeft_');
img.style.position=“相对”;
img.style.left=(div.offsetWidth-img.offsetWidth)/2+“px”;
img.style.top=(div.offsetHeight-img.offsetHeight)/2+“px”;
如果(带){
strip.style.position=“相对”;
strip.style.left=(div.offsetWidth-strip.offsetWidth)+“px”;
strip.style.top=(div.offsetHeight-strip.offsetHeight)+“px”;
}

由于至少有一个元素似乎是IMG元素,我猜在执行代码时该元素尚未完成加载


图像必须单独检索(通过浏览器),因此您必须确保只有在完成加载后才能读取它们的高度。

添加了代码块…知道原因吗?
$
?原型或jQuery是什么?@FaizulHasan大声呼喊,缓存这些引用。我已将改进的代码添加到您的问题中。。。“有时返回值,但有时不返回。”<是否使用某种“onload”"运行JavaScript的事件处理程序?在您的代码中,您正在读取三个不同元素的
offsetHeight
。这三个值都不正确吗?很抱歉造成混淆。onload在某种意义上不起作用,图像的宽度和高度计算正确。但其样式未设置为该图像。它的意思是:strip.style.position=“相对”strip.style.left=(div.offsetWidth-strip.offsetWidth)+“px”strip.style.top=(div.offsetHeight-strip.offsetHeight)+“px”;这部分代码块未执行或执行其他操作。但图像未定位到所需位置place@FaizulHasan在该行上设置一个断点,然后查看它是否执行。如果是,则查看分配了哪些值。