JQuery的偏移()将返回唯一的偏移只有HTML文档
问题描述:
我想要得到的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 + ")");
});
答
$(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/
感谢您的快速回复,我不知道我是如何错过了。 – ArJ 2013-04-22 13:45:44
@ArJ不客气!快乐的编码和DOM准备好了! :) – 2013-04-22 14:13:50