简体   繁体   中英

How can use a variable of Javascript in the “style” of tag?

i need create this function to improve my code. I need give to the a color given by the caller of the function, how can i do this?

Here the code:

function visualizza(array, div, color)
{
    div.html("");
    div.append("<p>");

    array.forEach(function (e)
    {
        div.append
        (
            "<span style='color: (here i need the variable)'>" + e.nominativo + "   Tel: "+e.cellulare+"</span><br>"
        )
    });

    div.append("</p>");
}

像其他js变量一样使用串联

"<span style='color: "+color+"'>" + e.nominativo + "   Tel: "+e.cellulare+"</span><br>"

You can use template literals which is a feature of ES2015. It enables you to inject variables into a string.

`<span style='color: ${yourColor}'>${e.nominativo} Tel: ${e.cellulare}</span><br>`

NOTE : This feature is not compatible with all browsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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