CSSOM视图模式offsetLeft/offsetTop属性实例页面

回到相关文章 »

代码:

HTML代码:
<div id="demo" class="demo">
    <button id="button1">点击弹出本按钮的offsetLeft和offsetTop</button><br /><br />
    <button id="button2">上按钮父标签应用position:relative</button>
</div>
                
JS代码:
document.getElementById("button1").onclick = function() {
    alert("offsetLeft: " + this.offsetLeft + ", \r\noffsetTop: " + this.offsetTop);
};
document.getElementById("button2").onclick = function() {
    document.getElementById("demo").style.position = "relative";
    this.innerHTML = "应用成功";
};
                

效果: