手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >jquery 设置元素相对于另一个元素的top值(实例代码)
jquery 设置元素相对于另一个元素的top值(实例代码)
摘要:sfdsfsddfsdf设置button在input的下方$("#button").css("{top":$("#input").offse...

<div id="span1">sfdsfsddfsdf</div>

<span id="span2">

<input id="input" type="text"></input>

<input id="button" type="button"></input>

</span>

设置button在input的下方

$("#button").css("{top":$("#input").offset().top-$("#span2").offset().top+$("#input").height,position:"absolute"});

这样舆论input在哪个位置button都在input的下边,同样可以运用到日历小插件在input文本框的下方

1、在jquery中offset().top是相对于body来说的,另外在设置top值的时候要找到与该元素最近的有相对值的元素

在js中可以这样写:

复制代码 代码如下:

//取得HTML控件绝对位置

Calendar.prototype.getAbsPoint = function (e){

var x = e.offsetLeft;

var y = e.offsetTop;

while(e = e.offsetParent){

x += e.offsetLeft;

y += e.offsetTop;

}

return {"x": x, "y": y};

}

var xy = this.getAbsPoint(popControl);

this.panel.style.left = xy.x + "px";

this.panel.style.top = (xy.y + dateObj.offsetHeight) + "px";

如图所示:

jquery 设置元素相对于另一个元素的top值(实例代码)1

【jquery 设置元素相对于另一个元素的top值(实例代码)】相关文章:

javascript动态设置样式style实例分析

jquery实现图片左右切换的方法

js实现顶部可折叠的菜单工具栏效果实例

jQuery获取字符串中出现最多的数

jquery实现弹出层效果实例

jQuery判断一个元素是否可见的方法

Jquery使用css方法改变样式实例

javascript获取select值的方法分析

jQuery子窗体取得父窗体元素的方法

JS实现简洁、全兼容的拖动层实例

精品推荐
分类导航