如何使用append-Div更改颜色



我不能用appenddiv动态更改颜色。我想更改div的默认颜色,红色。

我的HTML标记是:

<div id="risultato"></div>

Javascript代码:

var result= document.getElementById('risultato');
result.style.color = "red";
result.innerHTML+= <a href="www.google.com">

但是谷歌有蓝色而不是红色。

我也尝试过css:

<style>
#risultato {
margin-left: 18px;
font-size: 17px;
text-decoration: none;
color: red;
}   
</style>

但它不起作用。我该怎么做?

var result= document.getElementById('risultato');
result.style.color = "red";
result.innerHTML+= '<a style="color:green" href="www.google.com">google</a>'
both js and css are basically fine.  your innerHTML statement needs some fine tuning.
<div id="risultato">some text</div>

尝试使用:

<style>
#risultato a {
color:red;
}   
</style>

// Get the div
var result = document.getElementById('risultato');
// Append the element to the div and give it a class
result.innerHTML += "<a class='link' href='www.google.com'>Google</a>";
// Get the element (link)
let resultLink = document.querySelector('#risultato a');
// Style it
resultLink.style.color = 'red';
#risultato a{
font-size:17px;
}
<div id="risultato"></div>

最新更新