使用javascript更改css背景
问题描述:
我想使用js更改在css中设置为white
的background
颜色。不知道为什么没有发生。使用javascript更改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>
的.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";
答
您已经使用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>
答
document.querySelector(".invoice-box").style.background = "red";
如何使用querySelector
?
答
如果你可以使用jQuery只做为样本
$('#element').css('background-color', '#ffffff')
注:#ffffff
=白色
答
您使用类并调用ID在JavaScript,而不是使用
类