【问题标题】:Highcharts img in html title; onerror doesn't fire even with Highcharts Security AST sethtml标题中的Highcharts img;即使设置了 Highcharts Security AST,onerror 也不会触发
【发布时间】:2021-07-21 17:30:30
【问题描述】:

如果在 highcharts 标题/文本中找不到图像的 src,我需要运行 img 的 onerror 函数。

但是,highcharts 不支持开箱即用的 img 的 onerror,我需要将其添加到安全列表中,如 in the docs 所述。

例如

title: {
    useHTML:true,
    text: '<img src="/A/NotFound/src.png" onerror="doSomething(this)" />Solar Employment Growth'
}

我已尝试将 AST 设置为允许该属性;

Highcharts.AST.allowedAttributes.push('onerror');

但是,正如fiddle 中所见,该函数没有运行。 我错过了什么吗?

【问题讨论】:

    标签: javascript highcharts


    【解决方案1】:

    函数没有被触发,因为元素上没有src 属性。添加以下代码以允许将src 与空字符串引用一起使用。

    Highcharts.AST.allowedAttributes.push('onerror');
    Highcharts.AST.allowedAttributes.push('src');
    Highcharts.AST.allowedReferences.push('');
    

    现场演示: https://jsfiddle.net/BlackLabel/aocsn5p4/

    文档: https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting

    【讨论】:

    • 很好,成功了,谢谢。对于后代,有没有办法说明只有某些标签可以使用这些属性(即锁定 onerror 到 img 的唯一)?
    • 嗨@Matt Scott,目前似乎没有这种可能性,但您可以包装/覆盖默认的AST.filterUserAttributes 方法。
    猜你喜欢
    • 2011-04-25
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多