我将使用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>
显然,不幸的是,我无法以这种方式使用代码。但无论如何我都努力尝试,除此之外,我达到了一种无限的复杂性。它应该是解决这个问题的一种简单方法,对吗?
答案 0 :(得分:7)
你问题中的代码似乎有些混乱,所以我想给你一个我认为你试图做的例子。
首先考虑的是混合HTML,Javascript和CSS:
Why is using onClick() in HTML a bad practice?
我将删除内联内容并将其拆分为相应的文件。
接下来,我将使用“点击”事件并发布“更改”事件,因为您不清楚是否需要这两个事件。
您的函数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);
上
<击> 资料来源:w3schools
<击>CSS颜色使用十六进制(十六进制)表示法定义 用于红色,绿色和蓝色值(RGB)的组合。该 可以给予其中一个光源的最低值是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);
上
以下是进一步修改,允许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);
上
答案 1 :(得分:2)
根据您实际想要使用的事件(文本框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;
}
DEMO: http://jsfiddle.net/6pLUh/
按钮的一个小问题是它是表单中的提交按钮。单击时,提交表单(最终只是重新加载页面),重置JavaScript的任何更改。只需使用onchange
,您就可以根据输入更改颜色。
答案 2 :(得分:-1)
document.getElementById("fname").style.borderTopColor = 'red';
document.getElementById("fname").style.borderBottomColor = 'red';