javascript 使用 jquery 动态设置位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7862958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:33:31  来源:igfitidea点击:

Set position dynamically with jquery

javascriptjquerycss

提问by Thomas

I need to use the offset coordinates of one position to dynamically set the position of a second element ("#test").

我需要使用一个位置的偏移坐标来动态设置第二个元素(“#test”)的位置。

    var p = $("#desired_equity");
    var position = p.offset();

  $(document).ready(function(){
    $('#test').css("left", position.left);
    });

Im not sure what I am ding wrong here, any ideas?

我不确定我在这里做错了什么,有什么想法吗?

回答by genesis

You're maybe setting var p before DOM for #desired_equity is ready.

您可能在 #desired_equity 的 DOM 准备就绪之前设置 var p。

Try

尝试

$(document).ready(function(){
    var p = $("#desired_equity");
    var position = p.offset();   
    $('#test').css("left", position.left);
});