简体   繁体   English

如何使用 append Div 更改颜色?

[英]How can I change color with append Div?

I can't change color with append div dynamically.我无法动态更改 append div 的颜色。 I want change default color of div, in red.我想将 div 的默认颜色更改为红色。

My HTML markup is:我的 HTML 标记是:

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

Javascript code: Javascript 代码:

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

but google.com have blue color not red.但是 google.com 是蓝色而不是红色。

I also tried with css:我也试过 css:

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

But it doesn't work.但它不起作用。 How can I do this?我怎样才能做到这一点?

 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>

try with:尝试:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM