更改从脚本获取的特定单词的颜色
问题描述:
我试图改编一个代码来更改单个单词的颜色,但我不知道如何。更改从脚本获取的特定单词的颜色
原代码是
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>↓</b></font>';
if (crypt_single_getPar.ticker.change >= 0) {
crypt_single_text_style = '<font style="color:#009900;"><b>↑</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(在这种情况下,文本小部件)。如何做到这一点没有评估?
答
刘
感谢您的帮助。
随着时间的推移,我发现错误^^
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>↓</b></font>';
if (crypt_single_getPar.ticker.change >= 0) {
crypt_single_text_style = '<font style="color:#009900;"><b>↑</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
”'。一旦你的例子实际工作,我想我们会有一些进展:) –
答
这里是最后的结果^^
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>↓</b></font>';
if (crypt_single_getPar.ticker.change >= 0) {
crypt_single_text_style = '<font style="color:#009900;"><b>↑</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; }
感谢您的支持!
eval并非天生就是邪恶的,只是不安全,因为很多人不知道他们在做什么。如果你可以发布一个你想做的复制例子,它会更容易帮助你。 –
Hello A. Lau 感谢您的帮助。 – Criptonauta
发现错误!感谢eval提示! – Criptonauta