gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:12:58 24 4
gpt4 key购买 nike

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

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

最佳答案

您问题中的代码似乎有些困惑,所以我将举例说明您正在尝试做的事情。

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

Why is using onClick() in HTML a bad practice?

Unobtrusive Javascript

Inline Styles vs Classes

我将删除内联内容并将它们拆分到适当的文件中。

接下来,我将处理“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

Color Values

CSS colors are defined using a hexadecimal (hex) notation for the combination of Red, Green, and Blue color values (RGB). The lowest value that can be given to one of the light sources is 0 (hex 00). The highest value is 255 (hex FF).

Hex values are written as 3 double digit numbers, starting with a # sign.

更新:正如@Ian 所指出的

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

来源:W3C

Numerical color values

The format of an RGB value in hexadecimal notation is a ‘#’ immediately followed by either three or six hexadecimal characters. The three-digit RGB notation (#rgb) is converted into six-digit form (#rrggbb) by replicating digits, not by adding zeros. For example, #fb0 expands to #ffbb00. This ensures that white (#ffffff) can be specified with the short notation (#fff) and removes any dependencies on the color depth of the display.

这是一个替代函数,它将检查您的输入是否为有效的 CSS 十六进制颜色,它将仅设置文本颜色,如果无效则抛出警报。

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

/^#(?:[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

这是一个进一步的修改,将允许 colours by name连同十六进制。

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

关于Javascript 将文本和背景的颜色更改为输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16625618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com