相当于jquery赋值的纯javascript

Pure javascript equivalent to jquery assignment

本文关键字:javascript 赋值 jquery 相当于      更新时间:2023-09-26

我一直在努力找出JQuery代码的纯JavaScript版本的正确语法。

$('body').css('margin-bottom', $('footer').height());

到目前为止,我拥有的是:

var body = document.getElementById('#body');
var footer = document.getElementById('#footer');
body.style.marginBottom = footer.style.height;

只有当我在JavaScript中定义页脚高度时,此赋值才有效。

我知道这个问题——如何使用变量[duplicate]制作一个纯javascript.css()函数以及类似的问题,但仍然无法得到正确的答案。我知道我可以使用JQuery,但我看不出加载整个库来做非常简单的事情有什么价值。

提前谢谢。

打开JavaScript控制台并键入console.log(document.body.style)以查看document.body具有的所有样式属性。我还认为是document.body,而不仅仅是body

机身似乎具有以下裕度属性:

margin: ""
marginBottom: ""
marginLeft: ""
marginRight: ""
marginTop: ""

因此document.body.style.marginBottom = '15px'应该起作用。请记住,必须指定单位,如px

使用偏移高度:

body.style.marginBottom = footer.offsetHeight;