如何编辑我的 js,以便在输出输出之前清除表单

How do i edit my js so the form clears before putting out the output

本文关键字:输出 表单 清除 何编辑 编辑 js 我的      更新时间:2023-09-26

你好,我正在开发一个 html 颜色制作器,并在某处找到了一个 JavaScript,只有这个脚本 deosnt 当你按 make code 时清除输出

所以我的问题是我必须向代码中添加什么,以便在输入新结果之前清除输出

这是我的JavaScript:

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}
// -->

我猜:

frm.outtxt.value += multi_color_fade(txt, col_list);

应该是:

frm.outtxt.value = multi_color_fade(txt, col_list);

您希望更改值,而不是追加到该值

添加类似这样的东西,基本的javascript dom操作

element = document.getElementById(<control-id>);
element.innerHTML = "";
or element.value = "";