2017-04-07 66 views
1

我試圖改編一個代碼來更改單個單詞的顏色,但我不知道如何。更改從腳本獲取的特定單詞的顏色

原代碼是

crypt_single_getPar = eval('(' + httpGet("https://api.cryptonator.com/api/ticker/" + 
crypt_single_base_currency.match(/.*?\(([0-9a-z]+)\)/i)[1] + "-" + crypt_single_target_currency.match(/.*?\(([0-9a-z]+)\)/i)[1]) + ')'); 

if (crypt_single_getPar.success != false) { 
if (crypt_single_getPar.ticker.price > 1) 
    crypt_single_price = Math.round(crypt_single_getPar.ticker.price * 100)/100; 
else 
    crypt_single_price = crypt_single_getPar.ticker.price 

var crypt_single_text_style = '<font style="color:#cc0000;"><b>&darr;</b></font>'; 

if (crypt_single_getPar.ticker.change >= 0) { 
    crypt_single_text_style = '<font style="color:#009900;"><b>&uarr;</b></font>'; 
} 

document.write('\ 
<div class="cryptonatorwidget" style="border-radius:4px;padding:10px;' + crypt_single_style + '">\ 
<div><font style="font-size:90%;">' + crypt_single_base_currency + '</font>\ 
<b>' + crypt_single_price + '</b> ' + crypt_single_target_currency.match(/.*?\(([0-9a-z]+)\)/i)[1] +'\ 
</div>'); 

結果是所有白色。 (我不能後圖像由於沒有最低口碑)

Decred(DCR)電流值] BTC

而且我試圖改變顏色像這樣

Decred( DCR)電流值] [綠色] BTC [綠色]

我想達到的效果

document.write('\ 
<div class="cryptonatorwidget" style="border-radius:4px;padding:10px;' + crypt_single_style + '">\ 
<div><font style="font-size:90%;">' + crypt_single_base_currency + '</font>\ 
<b>' + crypt_single_price + '</b> ' + <span style="color:#2ED7A2;">' + crypt_single_target_currency.match(/.*?\(([0-9a-z]+)\)/i)[1] + '</span>\ 
</div>'); 

但隨後迪出現在網頁上。

任何人都可以幫助我嗎?

謝謝。

PS:另外,有人告訴我不要在wordpress中使用eval(在這種情況下,文本小部件)。如何做到這一點沒有評估?

+0

eval並非天生就是邪惡的,只是不安全,因爲很多人不知道他們在做什麼。如果你可以發佈一個你想做的複製例子,它會更容易幫助你。 –

+0

Hello A. Lau 感謝您的幫助。 – Criptonauta

+0

發現錯誤!感謝eval提示! – Criptonauta

回答

0

感謝您的幫助。

隨着時間的推移,我發現錯誤^^

crypt_single_display_time = false; 
 

 
function httpGet(theUrl) 
 
{ 
 
    var xmlHttp = null; 
 

 
    xmlHttp = new XMLHttpRequest(); 
 
    xmlHttp.open("GET", theUrl, false); 
 
    xmlHttp.send(null); 
 
    return xmlHttp.responseText; 
 
} 
 

 
function FormatNumberLength(num, length) { 
 
    var r = "" + num; 
 
    while (r.length < length) { 
 
     r = "0" + r; 
 
    } 
 
    return r; 
 
} 
 

 
crypt_single_base_currency = typeof crypt_single_base_currency == "undefined" ? "Bitcoin (BTC)" : crypt_single_base_currency; 
 
crypt_single_target_currency = typeof crypt_single_target_currency == "undefined" ? "US Dollar (USD)" : crypt_single_target_currency; 
 
crypt_single_background_color = typeof crypt_single_background_color == "undefined" ? "#FFFFFF" : crypt_single_background_color; 
 
crypt_single_transperency = typeof crypt_single_transperency == "undefined" ? true : crypt_single_transperency; 
 
crypt_single_border_width = typeof crypt_single_border_width == "undefined" ? 1 : crypt_single_border_width; 
 
crypt_single_border_color = typeof crypt_single_border_color == "undefined" ? "#CCCCCC" : crypt_single_border_color; 
 
crypt_single_border_corners = typeof crypt_single_border_corners == "undefined" ? "rounded" : crypt_single_border_corners; 
 
crypt_single_font_family = typeof crypt_single_font_family == "undefined" ? "Arial" : crypt_single_font_family; 
 
crypt_single_font_size = typeof crypt_single_font_size == "undefined" ? "medium" : crypt_single_font_size; 
 
crypt_single_font_color = typeof crypt_single_font_color == "undefined" ? "#000000" : crypt_single_font_color; 
 
crypt_single_display_time = typeof crypt_single_display_time == "undefined" ? true : crypt_single_display_time; 
 

 
crypt_single_getPar = eval('(' + httpGet("https://api.cryptonator.com/api/ticker/" + 
 
    crypt_single_base_currency.match(/.*?\(([0-9a-z]+)\)/i)[1] + "-" + crypt_single_target_currency.match(/.*?\(([0-9a-z]+)\)/i)[1]) + ')'); 
 

 
crypt_single_month_name = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
 

 
crypt_single_date = new Date(crypt_single_getPar.timestamp * 1000); 
 
crypt_single_day = FormatNumberLength(crypt_single_date.getDate(), 2); 
 
crypt_single_month = crypt_single_month_name[crypt_single_date.getMonth()]; 
 
crypt_single_year = crypt_single_date.getFullYear(); 
 
crypt_single_hour = FormatNumberLength(crypt_single_date.getHours(), 2); 
 
crypt_single_minutes = FormatNumberLength(crypt_single_date.getMinutes(), 2); 
 
crypt_single_seconds = FormatNumberLength(crypt_single_date.getSeconds(), 2); 
 
crypt_single_time_zone = "UTC+0:00"; 
 

 
if (crypt_single_transperency) 
 
    crypt_single_background_color = 'transparent'; 
 

 
switch (crypt_single_font_size) { 
 
    case 'small': 
 
     crypt_single_font_size = "90%"; 
 
     break; 
 

 
    case 'medium': 
 
     crypt_single_font_size = "110%"; 
 
     break; 
 

 
    case 'large': 
 
     crypt_single_font_size = "150%"; 
 
     break; 
 
} 
 

 
crypt_single_style = 'background: ' + crypt_single_background_color + 
 
    '; border: ' + crypt_single_border_width + 'px solid ' + crypt_single_border_color + ';' + 
 
    'font-family: ' + crypt_single_font_family +'; font-size: ' + crypt_single_font_size + 
 
    '; color: ' + crypt_single_font_color + ';'; 
 

 
if (crypt_single_border_corners == "square") { 
 
    crypt_single_style += "-webkit-border-radius: 0 !important; -moz-border-radius: 0 !important; border-radius: 0 !important;"; 
 
} 
 

 
if (crypt_single_getPar.success != false) { 
 
    if (crypt_single_getPar.ticker.price > 1) 
 
     crypt_single_price = Math.round(crypt_single_getPar.ticker.price * 100)/100; 
 
    else 
 
     crypt_single_price = crypt_single_getPar.ticker.price 
 

 
    var crypt_single_text_style = '<font style="color:#cc0000;"><b>&darr;</b></font>'; 
 

 
    if (crypt_single_getPar.ticker.change >= 0) { 
 
     crypt_single_text_style = '<font style="color:#009900;"><b>&uarr;</b></font>'; 
 
    } 
 

 
    document.write('\ 
 
<div class="cryptonatorwidget" style="border-radius:4px;padding:10px;' + crypt_single_style + '">\ 
 
<div><font style="font-size:90%;">' + crypt_single_base_currency + '</font>\ 
 
<b>' + crypt_single_price + '</b><span style="color:green";> ' + crypt_single_target_currency.match(/.*?\(([0-9a-z]+)\)/i)[1] +' </span>\ 
 
</div>'); 
 
} 
 
else { 
 
    document.write('\ 
 
    <div class="cryptonatorwidget" style="border-radius:4px;padding:10px;' + crypt_single_style + '">\ 
 
    <b>Nenhum preço disponível</b>\ 
 
    </div>'); 
 
} 
 

 
if (crypt_single_display_time && crypt_single_getPar.success != false) 
 
    document.write('<div style="font-size:70%;opacity:0.6;padding-top:10px;">\ 
 
      Updated ' + crypt_single_day + ' ' + crypt_single_month + ' ' + crypt_single_year + ' ' 
 
      + crypt_single_hour + ':' + crypt_single_minutes + ':' + crypt_single_seconds + ' ' 
 
      + crypt_single_time_zone + '\ 
 
     </div>'); 
 

 
document.write('<div id="cryptoticker" style="font-size:10px;opacity:0.6;padding-top:10px;"><span>Powered by <a href="https://www.arkhad.com" style="color:' + crypt_single_font_color + ';" target="_blank">Arkhad</a></span></div>\ 
 
</div>');
div#cryptoticker span { 
 
    display: none; } 
 
    
 

我失去了一些東西,但無法找到:/

+0

你可以把這個問題提出來嗎?只需點擊編輯按鈕並將其放在最後。 –

+0

您的代碼存在不少問題,因爲它無法運行。首先,我建議你把你的JS代碼放到http://jshint.com/找出錯誤。其次,如果你想使用多行文本,可以使用''''''或者將每個獨立的文本寫在一行上,但是將它們重複保存到一個變量中。 'long_string + =「

Next line
」'。一旦你的例子實際工作,我想我們會有一些進展:) –

0

這裏是最後的結果^^

crypt_single_base_currency = "Decred (DCR)";crypt_single_target_currency = "Bitcoin (BTC)";crypt_single_border_width = 0;crypt_single_font_family = "Sans-Serif";crypt_single_font_color = "#000";crypt_single_display_time = false; 
 

 
function httpGet(theUrl) 
 
{ 
 
    var xmlHttp = null; 
 

 
    xmlHttp = new XMLHttpRequest(); 
 
    xmlHttp.open("GET", theUrl, false); 
 
    xmlHttp.send(null); 
 
    return xmlHttp.responseText; 
 
} 
 

 
function FormatNumberLength(num, length) { 
 
    var r = "" + num; 
 
    while (r.length < length) { 
 
     r = "0" + r; 
 
    } 
 
    return r; 
 
} 
 

 
crypt_single_base_currency = typeof crypt_single_base_currency == "undefined" ? "Bitcoin (BTC)" : crypt_single_base_currency; 
 
crypt_single_target_currency = typeof crypt_single_target_currency == "undefined" ? "US Dollar (USD)" : crypt_single_target_currency; 
 
crypt_single_background_color = typeof crypt_single_background_color == "undefined" ? "#FFFFFF" : crypt_single_background_color; 
 
crypt_single_transperency = typeof crypt_single_transperency == "undefined" ? true : crypt_single_transperency; 
 
crypt_single_border_width = typeof crypt_single_border_width == "undefined" ? 1 : crypt_single_border_width; 
 
crypt_single_border_color = typeof crypt_single_border_color == "undefined" ? "#CCCCCC" : crypt_single_border_color; 
 
crypt_single_border_corners = typeof crypt_single_border_corners == "undefined" ? "rounded" : crypt_single_border_corners; 
 
crypt_single_font_family = typeof crypt_single_font_family == "undefined" ? "Arial" : crypt_single_font_family; 
 
crypt_single_font_size = typeof crypt_single_font_size == "undefined" ? "medium" : crypt_single_font_size; 
 
crypt_single_font_color = typeof crypt_single_font_color == "undefined" ? "#000000" : crypt_single_font_color; 
 
crypt_single_display_time = typeof crypt_single_display_time == "undefined" ? true : crypt_single_display_time; 
 

 
crypt_single_getPar = eval('(' + httpGet("https://api.cryptonator.com/api/ticker/" + 
 
    crypt_single_base_currency.match(/.*?\(([0-9a-z]+)\)/i)[1] + "-" + crypt_single_target_currency.match(/.*?\(([0-9a-z]+)\)/i)[1]) + ')'); 
 

 
crypt_single_month_name = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
 

 
crypt_single_date = new Date(crypt_single_getPar.timestamp * 1000); 
 
crypt_single_day = FormatNumberLength(crypt_single_date.getDate(), 2); 
 
crypt_single_month = crypt_single_month_name[crypt_single_date.getMonth()]; 
 
crypt_single_year = crypt_single_date.getFullYear(); 
 
crypt_single_hour = FormatNumberLength(crypt_single_date.getHours(), 2); 
 
crypt_single_minutes = FormatNumberLength(crypt_single_date.getMinutes(), 2); 
 
crypt_single_seconds = FormatNumberLength(crypt_single_date.getSeconds(), 2); 
 
crypt_single_time_zone = "UTC+0:00"; 
 

 
if (crypt_single_transperency) 
 
    crypt_single_background_color = 'transparent'; 
 

 
switch (crypt_single_font_size) { 
 
    case 'small': 
 
     crypt_single_font_size = "90%"; 
 
     break; 
 

 
    case 'medium': 
 
     crypt_single_font_size = "110%"; 
 
     break; 
 

 
    case 'large': 
 
     crypt_single_font_size = "150%"; 
 
     break; 
 
} 
 

 
crypt_single_style = 'background: ' + crypt_single_background_color + 
 
    '; border: ' + crypt_single_border_width + 'px solid ' + crypt_single_border_color + ';' + 
 
    'font-family: ' + crypt_single_font_family +'; font-size: ' + crypt_single_font_size + 
 
    '; color: ' + crypt_single_font_color + ';'; 
 

 
if (crypt_single_border_corners == "square") { 
 
    crypt_single_style += "-webkit-border-radius: 0 !important; -moz-border-radius: 0 !important; border-radius: 0 !important;"; 
 
} 
 

 
if (crypt_single_getPar.success != false) { 
 
    if (crypt_single_getPar.ticker.price > 1) 
 
     crypt_single_price = Math.round(crypt_single_getPar.ticker.price * 100)/100; 
 
    else 
 
     crypt_single_price = crypt_single_getPar.ticker.price 
 

 
    var crypt_single_text_style = '<font style="color:#cc0000;"><b>&darr;</b></font>'; 
 

 
    if (crypt_single_getPar.ticker.change >= 0) { 
 
     crypt_single_text_style = '<font style="color:#009900;"><b>&uarr;</b></font>'; 
 
    } 
 

 
    document.write('\ 
 
    <div class="cryptonatorwidget" style="border-radius:4px;padding:10px;' + crypt_single_style + '">\ 
 
    <div><font style="font-size:90%;">' + crypt_single_base_currency + '</font>\ 
 
    <b>' + crypt_single_price + '</b><span style="color:green";> ' + crypt_single_target_currency.match(/.*?\(([0-9a-z]+)\)/i)[1] +' </span>\ 
 
    </div>'); 
 
} 
 
else { 
 
    document.write('\ 
 
    <div class="cryptonatorwidget" style="border-radius:4px;padding:10px;' + crypt_single_style + '">\ 
 
    <b>Nenhum preço disponível</b>\ 
 
    </div>'); 
 
} 
 

 
if (crypt_single_display_time && crypt_single_getPar.success != false) 
 
    document.write('<div style="font-size:70%;opacity:0.6;padding-top:10px;">\ 
 
      Updated ' + crypt_single_day + ' ' + crypt_single_month + ' ' + crypt_single_year + ' ' 
 
      + crypt_single_hour + ':' + crypt_single_minutes + ':' + crypt_single_seconds + ' ' 
 
      + crypt_single_time_zone + '\ 
 
     </div>'); 
 

 
document.write('<div id="cryptoticker" style="font-size:10px;opacity:0.6;padding-top:10px;"><span>Powered by <a href="https://www.arkhad.com" style="color:' + crypt_single_font_color + ';" target="_blank">Arkhad</a></span></div>\ 
 
</div>');
div#cryptoticker span { 
 
    display: none; }

感謝您的支持!