我有一个jQuery和HTML的代码。该代码假设用户可以在单击时更改文本颜色,并选择文本应该更改的颜色。 但是,我有点麻烦。当我在textarea中写蓝色时,它总是将文本颜色改为黑色。第二个问题是当我点击“显示文本的颜色”时,它会用rgb显示颜色。有没有办法让它显示颜色的名字?无法更改文字颜色与jQuery同时获取用户颜色
下面是代码:
<!DOCTYPE html>
<html>
<head>
<title>Change Color of Text</title>
<script src="jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function() {
var TextColor = $("#text").css("color");
var ColorToChangeTo = $("#ColorToChange").text();
$("#GetColor").click(function() {
$("#ShowColor").text("The text color is " + TextColor);
});
$("#ChangeColor").click(function() {
$("#text").css("color", ColorToChangeTo);
});
});
</script>
</head>
<body dir="ltr" style="font-family: calibri;">
<center>
<div id="text" style="color: red;">
Hello, I am a text.
<br>Click the button below to see what is my color. If you want to change my color,
<br>enter the color that you want me to be displayed in and push the button "Change text color!"
</div>
<input type="button" id="GetColor" value="Show me the text's color!" />
<br>
<div id="ShowColor"></div>
<input type="text" id="ColorToChange" />
<br>
<input type="button" id="ChangeColor" value="Change text color!" />
</center>
</body>
</html>
非常感谢您! – Hagaymosko
不客气! :) – Bobby