如何在纯 JavaScript 中编写 $(“#content”).height()
How to write $("#content").height() in pure JavaScript?
在我的新项目中,我必须在没有jQuery的情况下做一些内容。如何在纯JavaScript中编写下面的jQuery代码?
$("#content").height()
当然,$("#content")
在 JS var content = document.getElementById('content');
中,但.height()
对我来说是一个大问题。请帮忙
等于 $('#content').height()
是:
document.getElementById('content').clientHeight;
或等于$('#content').css('height')
document.getElementById('content').style.height;
var content = document.getElementById("content");
content.clientHeight;
如评论中所述,adeneo的解决方案不正确,因为它会考虑不必要的填充到高度中。
要获得与jQuery的.height()
提供的相同的维度,这是您想要使用的代码。
const s = window.getComputedStyle(el, null),
height = el.clientHeight - parseInt(s.getPropertyValue('padding-top')) - parseInt(s.getPropertyValue('padding-bottom'));
这是一个函数,它将帮助计算jQuery的所有高度获取器函数。如果你想计算宽度,你只需要改变代码中的一些明显的属性。
function getHeight(el, type) {
if (type === 'inner') // .innerWidth()
return el.clientHeight;
else if (type === 'outer') // .outerWidth()
return el.offsetHeight;
const s = window.getComputedStyle(el, null);
if (type === 'height' || !type) // .height()
return el.clientHeight - parseInt(s.getPropertyValue('padding-top')) - parseInt(s.getPropertyValue('padding-bottom'));
else if (type === 'full') // .outerWidth( includeMargins = true )
return el.offsetHeight + parseInt(s.getPropertyValue('margin-top')) + parseInt(s.getPropertyValue('margin-bottom'));
return null;
}
相关文章:
- 分析高度属性时出现意外值{{specs.height}}.index.html
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- 在刚刚调用的append函数之后,jquery.height()不会返回实际值
- Phonegap css height%不适用于android 4.2-4.3
- $(document).height和$(window).heaght都返回相同的值-使用的是正确的doctype
- $(window).height() inside iframe?
- @Url.Content(“~”)不适用于localhost
- 节点,express应用程序返回text/javascript响应头(Content-Type),尽管设置了applic
- X-Content-Type-Options=nosniff解决方法
- document和window height返回相同的值
- 计算content中的数字总和使用Jquery可编辑文本
- HTML5画布y-height不正确
- TinyMCE/IE9/DIV with Width/Height/ContentEditable/如何阻止DIV选择
- 为什么不是't my css `content:`在多个浏览器中工作,并总体上提高跨浏览器兼容性
- 如果screen.height低于460px,请删除一个类
- Url.Content中的Url不匹配
- jQuery Height 无法正常工作
- Ajax content for Twitter Bootstrap Popover
- 在jQuery中将所有
content 替换为 - content
- 如何在纯 JavaScript 中编写 $(“#content”).height()