2013-04-22 58 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事件,包括這一點,你應該是好的。