我有一个带有自动完成功能的基本文本框,其中列出了彩虹的颜色,但我想更改它,以便当您选择一种颜色时,它会以该颜色出现(即,红色一词被涂成红色)但我不知道该怎么做。
代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function () {
var colours = [
"red",
"orange",
"yellow",
"green",
"blue",
"indigo",
"violet",
]
$("#rainbow1").autocomplete({
source: colours
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="rainbow1" style="color:red">Rainbow1: </label>
<input type="text" id="rainbow1" /> </br>
</div>
</body>
</html>
最佳答案
请参阅:http://jsfiddle.net/gkxe4/1/
$(function () {
var colours = [
"red",
"orange",
"yellow",
"green",
"blue",
"indigo",
"violet", ];
$("#rainbow1").autocomplete({
source: colours,
open: function (event, ui) {
$(".ui-autocomplete li > a").each(function () {
$(this).css('color', $(this).text());
});
},
select: function (event, ui) {
alert(ui.item.value);
$("#rainbow1").css("color", ui.item.value);
},
search: function () {
$(this).css('color', '');
}
});
});
关于jquery - 设置数组中文本的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15157966/