gpt4 book ai didi

javascript - 拾色器更新输入值

转载 作者:行者123 更新时间:2023-11-30 18:09:03 24 4
gpt4 key购买 nike

好吧,我在这上面花了一整天,但我遗漏了一些东西,只是想不通。我正在使用这个简单的颜色选择器 http://www.dynamicdrive.com/dynamicindex11/colorjack/index.htm

我想做的是更新背景颜色,然后更新字体颜色,让用户看到实时结果,并在文本输入框中自动更新十六进制颜色代码。下面是我的代码。

                            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Color Picker</title>
<link href="plugin.css" rel="stylesheet" type="text/css" />
<script src="plugin.js" type="text/JavaScript"></script>
</head>
<body>
<div id="plugin" onmousedown="HSVslide('drag','plugin',event)" style="TOP: 140px; LEFT: 430px; Z-INDEX: 20;">
<div id="plugHEX" onmousedown="stop=0; setTimeout('stop=1',100);">F1FFCC</div><div id="plugCLOSE" onmousedown="toggle('plugin')">X</div><br>
<div id="SV" onmousedown="HSVslide('SVslide','plugin',event)" title="Saturation + Value">
<div id="SVslide" style="TOP: -4px; LEFT: -4px;"><br /></div>
</div>
<form id="H" onmousedown="HSVslide('Hslide','plugin',event)" title="Hue">
<div id="Hslide" style="TOP: -7px; LEFT: -8px;"><br /></div>
<div id="Hmodel"></div>
</form>
</div>



<table>
<tr>
<td class="formlabel" align="right" valign="top">

<!-- color box to show background color and font color -->

<div id="currentcolor" style="margin-top:2px;padding:4px 10px 4px 10px;background-color:F1FFCC"><span id="showfontcolor" style="color:">Current Text Color</span></div>

<!-- End color box --></td>
<td valign="top"><table>
<tbody>
<tr>
<td><div class="small">Background Color</div>
<input name="colorcode" id="colorcode" value="" maxlength="199" class="textbox" style="width:80px" type="text">
<a href="javascript:toggle('plugin','colorcode');">Color Picker</a></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><div class="small">Text Color</div>
<input name="fontcolor" id="fontcolor" value="" maxlength="199" class="textbox" style="width:80px" type="text">
<a href="javascript:toggle('plugin','fontcolor');">Color Picker</a></td>
</tr>
</tbody>
</table></td>
</tr>
</table>


<script type="text/javascript">

//*** CUSTOMIZE mkcolor() function below to perform the desired action when the color picker is being dragged/ used
//*** Parameter "v" contains the latest color being selected
function mkColor(v){
//** In this case, just update DIV with ID="colorbox" so its background color reflects the chosen color

$S('currentcolor').background='#'+v;
$('colorcode').value='#'+v;
}

loadSV(); updateH('F1FFCC');
toggle('plugin');

</script>


</body>
</html>

问题是它不会实时更新输入 colorcodefontcolor

最佳答案

所以您的 javascript 有几处错误。下面我修复了你的代码:

function mkColor(v){
var field2update = $('#divUpdateID')[0].innerHTML;
if (field2update=='colorcode') {
$('#currentcolor').background='#'+v;
$('#colorcode').value='#'+v;
} else if (field2update=='fontcolor') {
$('#showfontcolor').color='#'+v;
$('#fontcolor').value='#'+v;
}
}

你所有的 jquery 选择器都坏了。此外,innerHTML 是一个 dom 函数而不是 jquery 函数,因此它需要用于 dom 对象而不是 jquery 对象数组。

以下是更新这些内容的方法。你必须弄清楚将它放在代码中的什么位置,因为我不太清楚它们应该放在哪里:

$("#currentcolor").html("The value it needs to be updated with!");
$("#colorcode").value = "Same as above!";

上面更新背景,下面更新你的fontcolor:

$("#fontcolor").value = "The value you want!";
$("#currentcolor").css("color", "your text color value here!"); //this line updates the color of the text in the `currentcolor` div;

编辑:

这是对上述代码的重写,也是对您使用的插件的更改:

对插件的更改:

var type_to_update = ''; //this goes in the global namespace,
...
function toggle(v, type){ //changed to accept which input should be updated
$S(v).display=($S(v).display=='none'?'block':'none');
type_to_update = type; //updating the global variable
}

更改您的代码:

function mkColor(v){
if(type_to_update == 'colorcode' || type_to_update == ''){ //default to changing background color if one of the pickers has not been clicked on.
$S('currentcolor').background = "#"+v;
$('colorcode').value = '#'+v;
}else if(type_to_update == 'fontcolor'){
$S('currentcolor').css("color", '#'+v);
$('fontcolor').value = '#'+v;
}

loadSV();
updateH('F1FFCC');
toggle('plugin');
}

关于javascript - 拾色器更新输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15077904/

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