8

我将使用 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;
}

查看上面的代码,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>

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

4

3 回答 3

9

您问题中的代码似乎有些混乱,所以我将举一个例子说明我认为您正在尝试做的事情。

首先要考虑的是混合 HTML、Javascript 和 CSS:

为什么在 HTML 中使用 onClick() 是一种不好的做法?

不显眼的 Javascript

内联样式与类

我将删除内联内容并将它们拆分为相应的文件。

接下来,我将使用“click”事件并释放“change”事件,因为不清楚你想要或需要两者。

您的函数changeBackground将背景颜色和文本颜色设置为相同的值(您的文本将不会被看到),所以我正在缓存颜色值,因为我们不需要在 DOM 中查找它两次。

CSS

#TheForm {
    margin-left: 396px;
}
#submitColor {
    margin-left: 48px;
    margin-top: 5px;
}

HTML

<form id="TheForm">
    <input id="color" type="text" />
    <br/>
    <input id="submitColor" value="Submit" type="button" />
</form>
<span id="coltext">This text should have the same color as you put in the text box</span>

Javascript

function changeBackground() {
    var color = document.getElementById("color").value; // cached

    // The working function for changing background color.
    document.bgColor = color;

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

document.getElementById("submitColor").addEventListener("click", changeBackground, false);

jsfiddle 上

资料来源:w3schools

颜色值

CSS 颜色是使用红色、绿色和蓝色颜色值 (RGB) 组合的十六进制 (hex) 表示法定义的。可以赋予其中一个光源的最低值是 0(十六进制 00)。最高值为 255(十六进制 FF)。

十六进制值写为 3 个两位数,以 # 符号开头。

更新:正如@Ian 所指出的

十六进制可以是 3 或 6 个字符长

资料来源:W3C

数值颜色值

十六进制表示法的 RGB 值的格式是一个“#”,后跟三个或六个十六进制字符。三位 RGB 符号 (#rgb) 通过复制数字而不是添加零转换为六位形式 (#rrggbb)。例如,#fb0 扩展为 #ffbb00。这确保了白色 (#ffffff) 可以用短符号 (#fff) 指定,并消除对显示器颜色深度的任何依赖。

这是一个替代功能,它将检查您的输入是否为有效的 CSS Hex Color,它将仅设置文本颜色,如果无效则发出警报。

对于正则表达式测试,我将使用此模式

/^#(?:[0-9a-f]{3}){1,2}$/i

但是如果您是正则表达式匹配并且想要将数字分成组,那么您将需要不同的模式

function changeBackground() {
    var color = document.getElementById("color").value.trim(),
        rxValidHex = /^#(?:[0-9a-f]{3}){1,2}$/i;

    if (rxValidHex.test(color)) {
        document.getElementById("coltext").style.color = color;
    } else {
        alert("Invalid CSS Hex Color");
    }
}

document.getElementById("submitColor").addEventListener("click", changeBackground, false);

jsfiddle 上

这是一个进一步的修改,它将允许按名称和按十六进制显示颜色。

function changeBackground() {
    var names = ["AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond", "Blue", "BlueViolet", "Brown", "BurlyWood", "CadetBlue", "Chartreuse", "Chocolate", "Coral", "CornflowerBlue", "Cornsilk", "Crimson", "Cyan", "DarkBlue", "DarkCyan", "DarkGoldenRod", "DarkGray", "DarkGrey", "DarkGreen", "DarkKhaki", "DarkMagenta", "DarkOliveGreen", "Darkorange", "DarkOrchid", "DarkRed", "DarkSalmon", "DarkSeaGreen", "DarkSlateBlue", "DarkSlateGray", "DarkSlateGrey", "DarkTurquoise", "DarkViolet", "DeepPink", "DeepSkyBlue", "DimGray", "DimGrey", "DodgerBlue", "FireBrick", "FloralWhite", "ForestGreen", "Fuchsia", "Gainsboro", "GhostWhite", "Gold", "GoldenRod", "Gray", "Grey", "Green", "GreenYellow", "HoneyDew", "HotPink", "IndianRed", "Indigo", "Ivory", "Khaki", "Lavender", "LavenderBlush", "LawnGreen", "LemonChiffon", "LightBlue", "LightCoral", "LightCyan", "LightGoldenRodYellow", "LightGray", "LightGrey", "LightGreen", "LightPink", "LightSalmon", "LightSeaGreen", "LightSkyBlue", "LightSlateGray", "LightSlateGrey", "LightSteelBlue", "LightYellow", "Lime", "LimeGreen", "Linen", "Magenta", "Maroon", "MediumAquaMarine", "MediumBlue", "MediumOrchid", "MediumPurple", "MediumSeaGreen", "MediumSlateBlue", "MediumSpringGreen", "MediumTurquoise", "MediumVioletRed", "MidnightBlue", "MintCream", "MistyRose", "Moccasin", "NavajoWhite", "Navy", "OldLace", "Olive", "OliveDrab", "Orange", "OrangeRed", "Orchid", "PaleGoldenRod", "PaleGreen", "PaleTurquoise", "PaleVioletRed", "PapayaWhip", "PeachPuff", "Peru", "Pink", "Plum", "PowderBlue", "Purple", "Red", "RosyBrown", "RoyalBlue", "SaddleBrown", "Salmon", "SandyBrown", "SeaGreen", "SeaShell", "Sienna", "Silver", "SkyBlue", "SlateBlue", "SlateGray", "SlateGrey", "Snow", "SpringGreen", "SteelBlue", "Tan", "Teal", "Thistle", "Tomato", "Turquoise", "Violet", "Wheat", "White", "WhiteSmoke", "Yellow", "YellowGreen"],
        color = document.getElementById("color").value.trim(),
        rxValidHex = /^#(?:[0-9a-f]{3}){1,2}$/i,
        formattedName = color.charAt(0).toUpperCase() + color.slice(1).toLowerCase();

    if (names.indexOf(formattedName) !== -1 || rxValidHex.test(color)) {
        document.getElementById("coltext").style.color = color;
    } else {
        alert("Invalid CSS Color");
    }
}

document.getElementById("submitColor").addEventListener("click", changeBackground, false);

jsfiddle 上

于 2013-05-18T15:23:48.800 回答
3

根据您实际要使用的事件(文本框change或按钮click),您可以尝试以下操作:

HTML:

<input id="color" type="text" onchange="changeBackground(this);" />
<br />
<span id="coltext">This text should have the same color as you put in the text box</span>

JS:

function changeBackground(obj) {
    document.getElementById("coltext").style.color = obj.value;
}

演示:http: //jsfiddle.net/6pLUh/

该按钮的一个小问题是它是一个表单中的提交按钮。单击时,会提交表单(最终只是重新加载页面),并且 JavaScript 的任何更改都会被重置。只需使用 ,onchange您就可以根据输入更改颜色。

于 2013-05-18T15:22:54.650 回答
-2
document.getElementById("fname").style.borderTopColor = 'red';
document.getElementById("fname").style.borderBottomColor = 'red';
于 2013-12-31T09:38:26.750 回答