[英]Why doesn't Chrome allow changing of style tag via JS?
I'm running this code: 我正在运行以下代码:
jQuery.get("http://email.hackmailer.com/checkuser.php?email=".concat(document.getElementById('name').value).concat(document.getElementById('domain').value), function(data) {
if(data == "true") {
document.getElementById('containerThree').style = "background-color:#20bb47;";
}else{
document.getElementById('containerThree').style = "background-color:#b33535;";
}
document.getElementById('avail').style = "color:#272727;";
document.getElementById('emt').style = "color:#272727;";
});
It works fine in FireFox, but in chrome not at all. 它在FireFox中可以正常工作,但在chrome中则不能。 I've tried using
.style.background = "#mycolorcode"
but it still doesn't work in chrome(and in that case, firefox too). 我试过使用
.style.background = "#mycolorcode"
但它在chrome中仍然无法使用(在这种情况下,也是Firefox)。
Try this: 尝试这个:
if (data === 'true') {
document.getElementById('containerThree').style.backgroundColor = '#20bb47';
} else {
document.getElementById('containerThree').style.backgroundColor = '#b33535';
}
http://devdocs.io/html/element/style http://devdocs.io/html/element/style
http://youmightnotneedjquery.com/ http://youmightnotneedjquery.com/
NOTE: 'true'
is a string. 注意:
'true'
是一个字符串。 You would most likely would rather use the Boolean true
. 您可能更愿意使用布尔值
true
。
Based on the latest edit to your question , does this cleanup of your surrounding code help? 根据对您问题的最新编辑 ,此清理周围的代码有帮助吗?
jQuery.get('http://email.hackmailer.com/checkuser.php?email='
.concat(document.getElementById('name').value)
.concat(document.getElementById('domain').value),
function (data) {
if (data === true) {
document.getElementById('containerThree').style.backgroundColor = '#20bb47';
} else {
document.getElementById('containerThree').style.backgroundColor = '#b33535';
}
document.getElementById('avail').style.color = '#272727';
document.getElementById('emt').style.color = '#272727';
});
You don't need to send a string as 'true' to check a condition. 您不需要发送字符串“ true”来检查条件。 Use it like:
像这样使用它:
var data = true; //use boolean but not 'true' as string.
Then you can simple use it as follows: 然后,您可以按如下所示简单使用它:
jQuery.get("http://email.hackmailer.com/checkuser.php?email=" + document.getElementById('name').value + document.getElementById('domain').value, function(data) {
var colorValue = "#272727";
document.getElementById('containerThree').style.backgroundColor = data == "true"?"#20bb47":"#b33535";
document.getElementById('avail').style.color = colorValue;
document.getElementById('emt').style.color = colorValue;
});
BTW, I am not sure how .style = "background-color:#20bb47;";
顺便说一句,我不确定
.style = "background-color:#20bb47;";
is working for you. 为你工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.