2013-04-22 56 views
1

我想要得到的div的偏移值,但我只得到HTML文档的IE 0,0JQuery的偏移()将返回唯一的偏移只有HTML文档

这里的测试代码偏移: - HTML: -

<div id="result">Click an element.</div> 
<div style="margin:auto; height:100px; width:134px; position:absolute; left: 193px; top: 53px;" id="name">JQuery</div> 

SCRIPT: -

$("*",document.body).mouseover(function (e) { 
    var offset = $(this).offset(); 
    e.stopPropagation(); 
    $("#result").text(this.tagName + " coords (" + offset.left + ", " + offset.top + ")"); 
}); 

回答

1

LIVE DEMO

$(function(){ // DOM IS NOW READY (document.ready shorthand) 

    $("*").click(function (e) { 
     var offset = $(this).offset(); 
     e.stopPropagation(); 
     $("#result").text(this.tagName + " coords (" + offset.left + ", " + offset.top + ")"); 
    }); 

}); 

在这里阅读更多:http://api.jquery.com/ready/

+0

感谢您的快速回复,我不知道我是如何错过了。 – ArJ 2013-04-22 13:45:44

+0

@ArJ不客气!快乐的编码和DOM准备好了! :) – 2013-04-22 14:13:50

0

你错过的jQuery .ready事件,包括这一点,你应该是好的。