更改文本颜色使用Javascript? [英] Change text color with Javascript?

查看:177
本文介绍了更改文本颜色使用Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的是单击按钮时更改标题的颜色。
这是我的code,但它不工作,我想不通,为什么不...

I want to change the color of a title when a button is clicked. This is my code, but it's not working and I can't figure out why not...

<div id="about">About Snakelane</div>

<input type="image" src="http://www.blakechris.com/snakelane/assets/about.png" onclick="init()" id="btn">

和我的JS ...

var about;   
function init() { 
    about = document.getElementById("about").innerHTML;
    about.style.color = 'blue';
}

在此先感谢!

推荐答案

您设置每个元素,而不是由它的内容的样式:

You set the style per element and not by its content:

function init() { 
  document.getElementById("about").style.color = 'blue';
}

使用的innerHTML 你/设置元素的内容。所以,如果你想修改你的标题,的innerHTML 将要走的道路。

With innerHTML you get/set the content of an element. So if you would want to modify your title, innerHTML would be the way to go.

在您的情况,但是,你只是想修改元素(将里面的文本的颜色)的属性,让你解决风格财产元素本身。

In your case, however, you just want to modify a property of the element (change the color of the text inside it), so you address the style property of the element itself.

这篇关于更改文本颜色使用Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆