0

こんにちは、私はhtmlカラーメーカーに取り組んでおり、makeコードを押したときにこのスクリプトだけが出力をクリアしないjavascriptをどこかに見つけました

私の質問は、新しい結果を入れる前に出力をクリアするために、コードに何を追加する必要があるかです

ここに私の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}

// -->
4

2 に答える 2

2

私は推測する:

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

次のようにする必要があります。

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

値を追加するのではなく、値を変更したい

于 2012-05-31T11:01:42.780 に答える
0

このようなものを追加します、基本的なjavascript dom操作

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

or element.value = "";
于 2012-05-31T11:01:34.490 に答える