是否可以获得克隆的 jQuery 对象的高度?
示例 jQuery:
$('document').ready(function(){
var $theP = $('p');
var $theDiv = $('div');
$theP.html($theP.html() + "<br>div height: " + $theDiv.outerHeight());
var $theClone = $theDiv.clone();
$theP.html($theP.html() + "<br>clonded div height: " + $theClone.outerHeight());
})
实时链接: http://jsbin.com/odujiv/4
在上面运行时,您会得到尝试获取其高度时结果为“0”。我认为这是因为克隆的对象尚未放入 DOM 中。获取高度的唯一方法是首先将克隆元素添加回 DOM 吗?如果是这样,我认为这是可行的,但最好在将其放回 DOM 之前处理它。
Sample jQuery:
$('document').ready(function(){
var $theP = $('p');
var $theDiv = $('div');
$theP.html($theP.html() + "<br>div height: " + $theDiv.outerHeight());
var $theClone = $theDiv.clone();
$theP.html($theP.html() + "<br>clonded div height: " + $theClone.outerHeight());
})
Live link: http://jsbin.com/odujiv/4
In running above, you get a result of '0' when trying to get its height. I think this is due to the fact that the cloned object hasn't yet been put into the DOM. Is the only way to get the height to first add the cloned element back to the DOM? If so, I think that's doable, but it'd be great to handle it before I put it back into the DOM.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不幸的是。原始 HTML 元素在未附加到文档时没有大小,并且 jQuery 尚未实现解决方法。我认为这是因为 HTML 元素的大小是相互依赖的!
No, unfortunately. Raw HTML elements don't have sizes when not attached to the document, and jQuery hasn't implemented a workaround. I assume that this is because of how interdependent HTML elements' sizes are on each other!
试试这个:
Try this: