【问题标题】:Escape variables HTML inside a function in JS [duplicate]JS中函数内的转义变量HTML [重复]
【发布时间】:2017-11-24 17:29:44
【问题描述】:

我需要在 JS 中的函数中为 html 转义变量,我有这样的东西:

function (foo1) {
   var cat = '<li id="'+ foo1.*** +'"></li>'
           + '<div aria-label="' + foo1.*** + '" tabindex = "0">'
           + '<button id="'+ foo1.*** +'"></button>'
           + '</div>'
}

这种逃脱能以某种方式完成吗?

感谢您的帮助。

【问题讨论】:

  • “html 的转义变量”是什么意思?

标签: javascript html tags escaping


【解决方案1】:

如果你想更优雅地解决这个问题,你可以使用 underscore.js 提供的模板引擎(来自文档):

var compiled = _.template("hello: <%= name %>");
compiled({name: 'moe'});
=> "hello: moe"

https://www.npmjs.com/package/jinja.js/。如果您使用框架,此功能可能已经内置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 2016-06-16
    • 1970-01-01
    • 2014-03-25
    • 2020-06-16
    • 2017-01-01
    • 2017-09-02
    相关资源
    最近更新 更多