您好,我想使用 jquery 根据其他 div 位置应用 div 的顶部和左侧位置。
jQuery(document).ready(function() {
var msg_top = jQuery(".color-cont").position().top;
var msg_left = jQuery(".color-cont").position().left;
jQuery(".messages").css({'top': msg_top + "50px", 'left': msg_left + "50px"});
});
});
html代码是:
<div class="color-cont">abc def ghi</div>
<div class="messages">apply on this</div>
您可以在此处的 jsfiddle 链接上进行编辑:http://jsfiddle.net/axpXU/
最佳答案
最后一行不需要,将msg_top + "50px"
改为(msg_top + 50) + "px"
jQuery(document).ready(function() {
var msg_top = jQuery(".color-cont").position().top;
var msg_left = jQuery(".color-cont").position().left;
jQuery(".messages").css({'top': (msg_top + 50) + "px", 'left': (msg_left+50) + "px"});
});
关于jquery - 如何使用jquery根据其他div应用顶部和左侧位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16956525/