2012-05-31 13 views
0

你好IM上的HTML染料生產商工作之前清除,並發現了一個JavaScript的地方僅此腳本deosnt清除掉輸出,當您按下使代碼如何編輯我的js這樣的形式撲滅輸出

所以我問題是我有什麼要添加到我的代碼,以便它把新成果

這裏之前清除輸出是我的javascript:

function is_valid(color_list) 
{ 
var colors = (color_list.toLowerCase()).split(','); 
var hex = 'abcdef'; 
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 = 'abcdef'; 

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} 

// --> 
+0

你可以很具體一點嗎?請在您的代碼中提及您遇到問題的行,或者在JS小提琴上張貼它。 –

回答

2

我想:

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

應該是:

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

你想改變的價值,而不是附加給它

+0

thnx這一個工作:P –

0

添加這樣的事情,基本的JavaScript DOM操作

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

or element.value = "";