如何使用 append Div 更改颜色?
How can I change color with append Div?
我无法通过动态追加 div 更改颜色。我想将 div 的默认颜色更改为红色。
我的 HTML 标记是:
<div id="risultato"></div>
Javascript代码:
var result= document.getElementById('risultato');
result.style.color = "red";
result.innerHTML+= <a href="www.google.com">
但是google.com有蓝色而不是红色。
我也试过 css:
<style>
#risultato {
margin-left: 18px;
font-size: 17px;
text-decoration: none;
color: red;
}
</style>
但是没用。我该怎么做?
试试:
<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>
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>
我无法通过动态追加 div 更改颜色。我想将 div 的默认颜色更改为红色。
我的 HTML 标记是:
<div id="risultato"></div>
Javascript代码:
var result= document.getElementById('risultato');
result.style.color = "red";
result.innerHTML+= <a href="www.google.com">
但是google.com有蓝色而不是红色。
我也试过 css:
<style>
#risultato {
margin-left: 18px;
font-size: 17px;
text-decoration: none;
color: red;
}
</style>
但是没用。我该怎么做?
试试:
<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>
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>