javascript - document.body.style.margin 移除元素

标签 javascript html css

<分区>

我正在玩一些 JS,想知道为什么在为我的 body 边缘添加样式时我的其他元素消失了?

window.onload = function() {

with(document.body.style) {
  margin = "0px 0px 0px 0px";
}

var header = document.createElement('header');

with(header.style) {
  height = '2px';
  marginBottom = '30px';
  backgroundColor = '#333';
  width = '100%';
  margin = '0';
  padding = '0';
}

document.body.appendChild(header);

var h1 = document.createElement('h1');
var name = document.createTextNode("Header1");
h1.appendChild(name);

document.body.appendChild(h1);

}

我想做的是移除应用于任何元素的边距和填充。在 CSS 中我会这样做:

* { margin: 0; padding: 0; }

最佳答案

我删除了 with() 并直接引用了样式属性,这解决了问题并且样式现在可以正确应用。

关于javascript - document.body.style.margin 移除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14262114/

上一篇:php - 使用 PHP 创建图形类

下一篇:css - 在 HTML5 的文章中使用主要内容持有者?

相关文章:

javascript - 使用 AngularJS 表达式设置输入类型 ="file"在 Chrome 中不起作用

jQuery 如果 H1 :contains a word do something

只有在选项卡/浏览器窗口获得焦点时,javascript 才应该运行

javascript - 如何使用 jquery 和 javascript 显示数据库中的菜单项

javascript - 如何创建表单添加按钮

javascript - amStock 绘制多个数据集图表 : Get names of all the Dataset selected for comparing

javascript - Google 图表 - 第 138 行 loader.js 繁忙时出现错误

jquery - CSS 不适用于响应式设计的菜单

html - 如果使用 Javascript 输入和选择框为空,如何检查页面加载

ios - 移动网站无法在 IOS 11+ 上运行