簡體   English   中英

Chrome為什么不允許通過JS更改樣式標簽?

[英]Why doesn't Chrome allow changing of style tag via JS?

我正在運行以下代碼:

    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;";
    });

它在FireFox中可以正常工作,但在chrome中則不能。 我試過使用.style.background = "#mycolorcode"但它在chrome中仍然無法使用(在這種情況下,也是Firefox)。

嘗試這個:

if (data === 'true') {
  document.getElementById('containerThree').style.backgroundColor = '#20bb47';
} else {
  document.getElementById('containerThree').style.backgroundColor = '#b33535';
}

http://devdocs.io/html/element/style

http://youmightnotneedjquery.com/

注意: 'true'是一個字符串。 您可能更願意使用布爾值true

根據對您問題的最新編輯 ,此清理周圍的代碼有幫助嗎?

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';

});

您不需要發送字符串“ true”來檢查條件。 像這樣使用它:

var data = true; //use boolean but not 'true' as string.

然后,您可以按如下所示簡單使用它:

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;
});

順便說一句,我不確定.style = "background-color:#20bb47;"; 為你工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM