2017-09-24 130 views
0

我想使用js更改在css中设置为whitebackground颜色。不知道为什么没有发生。使用javascript更改css背景

的.html
<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> 
的.css
.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; 
的.js
document.getElementById("invoice-box").style.background = "red"; 

回答

0

您已经使用getElementById它取HTML元素的ID,但你已经定义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> 
1
document.querySelector(".invoice-box").style.background = "red"; 

如何使用querySelector

0

如果你可以使用jQuery只做为样本

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

注:#ffffff =白色

0

您使用类并调用ID在JavaScript,而不是使用