简体   繁体   English

如何使用 javascript 更改 HTML 元素的边框颜色

[英]How to change the border colour of an HTML element using javascript

I have some JSON data displayed on my HTML page.我的 HTML 页面上显示了一些 JSON 数据。 I want the border color of #row to change depending on the data.我希望 #row 的边框颜色根据数据而变化。 I have written this but it doesn't seem to be working.我已经写了这个,但它似乎不起作用。 There's no error message but the border doesn't show up.没有错误消息,但没有显示边框。 How can I fix it?我该如何解决?

 function displaylineColour(birds){
    let colour ="";
    if(birds == "Not Threatened"){
        colour= "#02a028";
    }else if(birds == "Naturally Uncommon"){
        colour="#649a31";
    }else if(birds == "Relicit"){
        colour="#99cb68"; 
    }else if(birds == "Recovering"){
        colour="#fecc33";
    }else if(birds == "Declining"){
        colour="#fe9a01";
    }else if(birds == "Nationally Increasing"){
        colour="#c26967";
    }else if(birds == "Nationally Vulnerable"){
        colour="#9b0000";
    }else if(birds == "Nationally Endangered"){
        colour="#660032";
    }else if(birds == "Nationally Critical"){
        colour="#320033";
    }else if(birds == "Extinct"){
        colour="#000";
    }else if(birds == "Data Deficient"){
        colour="#000";

    }
    document.getElementById("#row").style.borderBottom = "3px solid colour"
}

in the bottom line, you have two mistakes归根结底,你有两个错误

  1. you wrote document.getElementById("#row"), its not supposed to have a #你写了 document.getElementById("#row"),它不应该有 #
  2. you just had the string "3px solid colour" with no relation to the colour variable你只是有与颜色变量无关的字符串“3px纯色”
document.getElementById("row").style.borderBottom = "3px solid" = colour

or或者

document.getElementById("row").style.borderBottom = `3px solid ${colour}`

document.getElementById("#row").style.borderBottom = "3px solid " + colour document.getElementById("#row").style.borderBottom = "3px 实心" + 颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM