Javascript 如何编辑我的js,以便在输出前清除表单
您好,我在一个html颜色生成器上工作,在某个地方发现了一个javascript,只有这个脚本在您按下make code时无法清除输出 所以我的问题是,我必须向代码中添加什么,以便在输入新结果之前清除输出 以下是我的javascript:Javascript 如何编辑我的js,以便在输出前清除表单,javascript,Javascript,您好,我在一个html颜色生成器上工作,在某个地方发现了一个javascript,只有这个脚本在您按下make code时无法清除输出 所以我的问题是,我必须向代码中添加什么,以便在输入新结果之前清除输出 以下是我的javascript: function is_valid(color_list) { var colors = (color_list.toLowerCase()).split(','); var hex = '0123456789abcdef'; var valid =
function is_valid(color_list)
{
var colors = (color_list.toLowerCase()).split(',');
var hex = '0123456789abcdef';
var valid = true;
for (var i = 0; (i < colors.length) && valid; i++)
{
if (colors[i].length != 6) valid = false;
for (var j = 0; j < colors[i].length; j++)
{
if (hex.indexOf(colors[i].charAt(j)) < 0) valid = false;
}
}
return valid;
}
function hex_to_dec(n)
{
// n will always be passed in as a string
var c = '0123456789abcdef';
n = n.toLowerCase();
return c.indexOf(n.charAt(0)) * 16 + c.indexOf(n.charAt(1));
}
function hex_to_rgb(hex)
{
// hex will always be passed in as a string
var srgb = '';
for (var i = 0; i < 6; i += 2)
srgb = srgb + hex_to_dec(hex.substring(i, i + 2)) + ',';
return srgb.substring(0, srgb.length - 1);
}
function two_color_fade(txt, color1, color2)
{
var htm = '';
if (txt.length == 0) return htm;
// color1 and color2 are strings from the comma delimited color_list
// s1 and s2 are now in form 255,00,33
var s1 = hex_to_rgb(color1);
var c1 = s1.split(',');
var s2 = hex_to_rgb(color2);
var c2 = s2.split(',');
for (var i = 0; i < 3; i++)
{
c1[i] = parseInt(c1[i]);
c2[i] = parseInt(c2[i]);
}
var inc = new Array(3);
for (var i = 0; i < 3; i++)
{
if (txt.length - 1 > 0)
{
inc[i] = (c2[i] - c1[i]) / (txt.length - 1);
}
else
inc[i] = c2[i];
}
for (var i = 0; i < txt.length; i++)
{
htm = htm + '<font color="' + rgb_to_hex(c1[0], c1[1], c1[2]) + '">' + txt.charAt(i) + '<\/font>';
// test print out
//htm += '<font color="' + rgb_to_hex_wtv(c1[0], c1[1], c1[2]) + '">' +
//i + ': ' + c1[0] + ',' + c1[1] + ',' + c1[2] + '<\/font><br>';
for (var j = 0; j < 3; j++) c1[j] += inc[j];
}
return htm;
}
function multi_color_fade(txt, color_list)
{
var htm = '';
var color_count = 0;
var chunk_count = 0;
var chunks = new Array();
var colors = (color_list.toLowerCase()).split(',');
var num_chunks = colors.length - 1;
var quotient = Math.floor(txt.length / num_chunks);
var remainder = txt.length % num_chunks;
for (var i = 0; i < num_chunks; i++)
{
chunks[i] = quotient;
if (i >= (num_chunks - remainder)) chunks[i]++;
}
for (var i = 0; i < txt.length; i += chunks[chunk_count++])
{
// The substr method would be eaiser, but WebTV can't handle substr.
// str.substr(i, j) is same as str.substring(i, i + j)
htm += two_color_fade(txt.substring(i, i + chunks[chunk_count]), colors[color_count], colors[color_count + 1]);
color_count++;
}
return htm;
}
function fade(frm)
{
var col_list = '';
var txt = frm.intxt.value;
// preset or custom color scheme?
col_list = frm.selPresets.options[frm.selPresets.selectedIndex].value;
if (txt.length == 0)
{
alert('You need to enter some text.');
frm.intxt.focus();
return;
}
frm.outtxt.value += multi_color_fade(txt, col_list);
}
function rgb_to_hex(r, g, b)
{
// r, g, and b are numbers, not strings
// correct function returns 255 if n > 255 and 0 if n < 0
// The "correct" function doesn't really need to be called, since
// rgb_to_hex will take care of anything < 256 or > -1, but just
// to be on the safe (WebTV) side...
r = correct(r);
g = correct(g);
b = correct(b);
var h = ((r << 16) | (g << 8) | (b)).toString(16);
while (h.length < 6) h = '0' + h;
return '#' + h;
}
function correct(n){if(n>255)return 255;if(n<0)return 0;return n}
// -->
函数有效(颜色列表)
{
var colors=(color_list.toLowerCase()).split(',');
var hex='0123456789abcdef';
var valid=true;
对于(var i=0;(i0)
{
inc[i]=(c2[i]-c1[i])/(txt.length-1);
}
其他的
inc[i]=c2[i];
}
对于(变量i=0;i';
对于(var j=0;j<3;j++)c1[j]+=inc[j];
}
返回htm;
}
功能多色彩淡入(txt,色彩列表)
{
var htm='';
var color_count=0;
var chunk_count=0;
var chunks=新数组();
var colors=(color_list.toLowerCase()).split(',');
var num_chunks=colors.length-1;
var商=Math.floor(txt.length/num_chunks);
var rements=txt.length%num_chunks;
for(var i=0;i=(num_chunks-余数))块[i]++;
}
for(var i=0;i255,则correct函数返回255;如果n<0,则返回0
//实际上不需要调用“correct”函数,因为
//rgb_to_hex将处理<256或>-1的任何内容,但仅
//为了安全起见(网络电视)。。。
r=正确(r);
g=正确(g);
b=正确(b);
var h=((r添加如下内容,基本的javascript dom操作
element = document.getElementById(<control-id>);
element.innerHTML = "";
or element.value = "";
element=document.getElementById();
element.innerHTML=“”;
或element.value=“”;
我想:
frm.outtxt.value += multi_color_fade(txt, col_list);
应该是:
frm.outtxt.value = multi_color_fade(txt, col_list);
您想更改值,而不是附加到它上您能说得非常具体吗?请在代码中指出您遇到问题的那一行,或者将其发布到JS FIDLE上。