简体   繁体   中英

Correct HTML syntax in JAVASCRIPT

My problem is that I don't know the correct syntax for implementing html in a javascript snippet.

I'm using simplecart.js and I would like to add a custom cart column with the color of one item in it, but I'm stuck with this since one hour.

{
    view: function (item, column) {
        "<div id='couleur'>
        <div style='background-color:'"
        return item.get('color')"'></div>
    </div>"
    },
    label: "Couleur"
},

Could anyone help me with this?
Thanks a lot.

You can not have line breaks in a string, you have nested double quotes, snd you have a function call that makes no sense.

view: function (item, column) {
    var str = "<div id='couleur'>" +
              "<div style='background-color:" + item.get('color') + "'></div>" +
              "</div>";
    return str;
}

Most people would use more of a templating type of architecture.

view: function (item, column) {
    var str = "<div id='couleur'><div style='background-color:{color}'></div></div>";
    return str.replace("{color}", item.get('color'));
}

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