更改从脚本中获取的特定单词的颜色

时间:2017-04-07 04:28:19

标签: javascript wordpress

我正在尝试调整代码来改变单个单词的颜色,但我不知道如何。

原始代码是

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(在本例中是一个文本小部件)。没有eval怎么办?

2 个答案:

答案 0 :(得分: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; }
    
 

我遗失了一些但找不到的东西:/

答案 1 :(得分:0)

这是最终结果^^

&#13;
&#13;
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>');
&#13;
div#cryptoticker span {
    display: none; }
&#13;
&#13;
&#13;

感谢您的支持!