用javascript改变css背景

时间:2017-09-24 04:47:31

标签: javascript css background-color

我想使用js来更改css中设置为background的{​​{1}}颜色。不知道为什么没有发生任何事情。

html的
white
的CSS
<body>
  <div class="invoice-box">
    <table cellpadding="0" cellspacing="0">
      <tr class="top">
        <td colspan="2">
          <table>
            <tr>
              <td class="title">
                <img src="emoji.png" width="100">
              </td>
.js文件
.invoice-box {
  background: white;
  max-width:800px;
  margin:auto;
  padding:30px;
  border:1px solid #eee;
  box-shadow:0 0 10px rgba(0, 0, 0, .15);
  font-size:16px;
  line-height:24px;
  font-family:'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;
    color:#555;

4 个答案:

答案 0 :(得分:1)

document.querySelector(".invoice-box").style.background = "red";

如何使用querySelector

答案 1 :(得分:0)

您已使用getElementById从Id中获取html元素,但您已将invoice-box定义为类。将其定义为id

<body>
<div class="invoice-box" id="invoice-box">
    <table cellpadding="0" cellspacing="0">
        <tr class="top">
            <td colspan="2">
                <table>
                    <tr>
                        <td class="title">
                            <img src="emoji.png" width="100">
                        </td>

答案 2 :(得分:0)

如果你可以使用jquery,只需做样本

$('#element').css('background-color', '#ffffff') 

注意:#ffffff =白色

答案 3 :(得分:0)

你在javascript中使用类和调用id,而不是使用class