最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript change html or body css - Stack Overflow

programmeradmin5浏览0评论

I use in my webpage the css description

    body {
    font-size: 12px;
    font-family: Sans-Serif;
    background: url(images/diffdummy.png) no-repeat center center fixed;
    overflow: hidden;
    display: block;
    margin-left: auto;
    margin-right: auto;
}

How I can remove the background image with java script on runtime? I tried

document.getElementById("html").style.background="";
document.getElementsByTagName("html").style.background="";
document.getElementsByTagName("html")[0].style.background="";

But nothing is working. Anybody here who can give me a hint?

I use in my webpage the css description

    body {
    font-size: 12px;
    font-family: Sans-Serif;
    background: url(images/diffdummy.png) no-repeat center center fixed;
    overflow: hidden;
    display: block;
    margin-left: auto;
    margin-right: auto;
}

How I can remove the background image with java script on runtime? I tried

document.getElementById("html").style.background="";
document.getElementsByTagName("html").style.background="";
document.getElementsByTagName("html")[0].style.background="";

But nothing is working. Anybody here who can give me a hint?

Share Improve this question asked Jan 28, 2019 at 18:11 IngoIngo 891 gold badge2 silver badges6 bronze badges 2
  • document.body.style.background = 'none' – Calvin Nunes Commented Jan 28, 2019 at 18:16
  • Class is set on the body, but you are selecting the html element? – epascarello Commented Jan 28, 2019 at 18:20
Add a ment  | 

2 Answers 2

Reset to default 15

Why html when you are using body as your CSS selector?

just use:

document.getElementsByTagName('body')[0].style.background = "none";

or

document.body.style.background = "none";

Code in action!

// removing background-color from body
document.getElementsByTagName('body')[0].style.background = "none";
body {
  height: 100px;
  width: 100%;
  background-color: red;
}

div {
  height: 40px;
  width: 20%;
  background-color: green;
}
<body>
 <div></div>
</body>

just to remove the background image should do like:

document.body.style.backgroundImage = "none";

to avoid i.e. removing background-color or other css props that might be applied to just background

发布评论

评论列表(0)

  1. 暂无评论