小编Hol*_*erg的帖子

Javascript将文本和背景的颜色更改为输入值

我将使用javascript来创建一个函数来同时更改背景颜色以及文本 - 基于文本输入的值.我已经改变了背景颜色,但无法设法使文本正常工作.

function changeBackground() {
    // The working function for changing background color.
    document.bgColor = document.getElementById("color").value;

    // The code I'd like to use for changing the text simultaneously - however it does not work.
    document.getElementById("coltext").style.color = document.getElementById("color").value;
}
Run Code Online (Sandbox Code Playgroud)

查看上面的代码,document.getElementById("coltext").style.color = x当我输入实际颜色而不是"颜色"值时,文本的代码会起作用.

这是我所指的html(我知道它可怕的优化,但它正在进行中):

<form id="TheForm" style="margin-left:396px;">
    <input id="color" type="text" onchange="changeBackground();" />
    <br/><input id="submitColor" value="Submit" type="button" onclick="changeBackground();" style="margin-left:48px; margin-top:5px;" />
</form>

<span id="coltext">This text should have the same color as you put in the text box</span>
Run Code Online (Sandbox Code Playgroud)

显然,不幸的是,我不能以这种方式使用代码.但无论如何我都努力尝试,除此之外,我达到了一种无限的复杂性.它应该是解决这个问题的一种简单方法,对吧?

javascript text colors submit

6
推荐指数
1
解决办法
10万
查看次数

标签 统计

colors ×1

javascript ×1

submit ×1

text ×1