简体   繁体   English

Chrome为什么不允许通过JS更改样式标签?

[英]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.

相关问题 Polymer 不允许在 Chrome 中设置内部元素样式和传播事件 - Polymer doesn't allow to style inner elements and propagate events in Chrome 为什么`style.webKitTransition`和`style.webkitTransform`在Chrome中不再起作用? - Why doesn't `style.webKitTransition` and `style.webkitTransform` work anymore in Chrome? 使用JS和HTML可以更改样式,但JS和CSS并不总是 - Changing style using JS & HTML works, JS & CSS doesn't always 为什么这个JS函数有问题? Firefox可以工作,但Chrome不能工作 - Why is wrong with this JS function? Firefox does work but Chrome doesn't js 样式不更新 - js style doesn't update 将项目入口点更改为公用文件夹后样式和 js 不起作用 - Style and js doesn't work after changing project entry point to public folder MediaElement.js音频标签没有动态添加样式 - MediaElement.js audio tag doesn't get style added dynamic <a>标记上的</a>下载属性<a>在Chrome中不起作用</a> - download attribute on <a> tag doesn't work in Chrome 为什么我的CSS样式没有变化? - Why isn't my css style changing? 为什么在有Chrome浏览器和Firefox的情况下snapsvg掩码不起作用 <base> 标头中的标签? - Why snapsvg mask doesn't work in Chrome and Firefox when there's a <base> tag in the header?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM