jQuery.css ('top' ) 和 IE8

标签 jquery css internet-explorer-8 styles

我对 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/

相关文章:

html - 在下拉列表 HTML 中有一个滚动条

javascript - AngularJS 1.3 页面不会在 IE8 中加载

jquery - 从文本框中删除自动搜索并在搜索按钮上搜索

javascript - 在 spring mvc 中通过 AngularJs 提交表单

html - Bootstrap 图像在 safari 中没有响应

html - 如何使用CSS来打破文本行

javascript - 在不同的 html 页面上用另一个 div 替换一个 div

jquery - css/jquery - 如果不是,如何隐藏最后一行 float 元素

internet-explorer-8 - CSS 圆 Angular IE8 和 IE7

javascript - IE8后退按钮和动态内容