我对 IE8 + jQuery.css('top') 函数有疑问。
1) 我有一个高度为 100 像素、宽度为 600 像素的 DIV。我在第一个 style="top:50%"中有另一个 DIV,所以它应该放在 top=50px 中。
2) 所有浏览器都能正确显示。但是,稍后,我需要使用 jQuery 来查找元素的位置,这就是 IE8 的问题:
$('#txt').css('top') 返回的不是 50px 而是 300px !!!!!这是不正确的。我还注意到它总是返回父 DIV 的 0.5*WIDTH 值!!这让我发疯!怎么会这样?
这里是代码示例:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function onLoadTest(){
alert("document.getElementById('txt').style.top="+document.getElementById('txt').style.top+"\n$(#txt).css('top')="+$('#txt').css('top'));
}
</script>
</head>
<body onload="onLoadTest()" style="padding: 0px; margin: 0px; width:100%; height:100%">
<div id="box" style="width:600px; height:100px; position:absolute; padding:0px; margin:0px; border: solid 1px">
<div id="txt" style="top:50%; position:relative; padding:0px; margin:0px;border: solid 1px">
im in the position top=50% (which is 50px), plz compare to alert box data
</div>
</div>
</body>
</html>
最佳答案
这是我的 fiddle .尝试使用 offset()。 IE 有时会变得很奇怪。 使用 offset 与你 parent 的寄宿生一起使用会让你得到 51,所以我把寄宿生从 parent 那里拿走了
关于jQuery.css ('top' ) 和 IE8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9994679/