javascript - 使用 TypeScript 创建 SVG 元素

标签 javascript svg typescript tslint

我正在尝试在 TypeScript 中创建格式正确的 SVG 元素:

createSVGElement(tag) {
    return document.createElementNS("http://www.w3.org/2000/svg", tag);
}

但是,我在 tslint

中收到以下错误

Forbidden http url in string: 'http://www.w3.org/2000/svg'

如何避免这个错误?我认为我需要此 URL 才能满足 SVG 标准?

最佳答案

tslint 由于 no-http-string 规则而提示。规则定义为:

Do not use strings that start with 'http:'. URL strings should start with 'https:'. Http strings can be a security problem and indicator that your software may suffer from cookie-stealing attacks. Since version 1.0, this rule takes a list of regular expressions as a parameter. Any string matching that regular expression will be ignored. For example, to allow http connections to example.com and examples.com, configure your rule like this: "no-http-string": [true, "http://www.example.com/?.", "http://www.examples.com/?."]

(来源:https://github.com/Microsoft/tslint-microsoft-contrib/blob/master/README.md)

因此,假设您信任万维网联盟,您可以在 tslint.json 中给它一个异常(exception),例如

"no-http-string": [true, "http://www.w3.org/.*"]

关于javascript - 使用 TypeScript 创建 SVG 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40224636/

相关文章:

javascript - 在 CSS 中定位 DIV 的问题

javascript - 我怎样才能使一个盒子适合 css 中的图片(观看视频以了解我的意思)

css - 如何将 transform-origin 与 SVG 结合使用?

reactjs - 如何解决语法错误 : Unexpected token 'export' in react nextjs project?

javascript - 尝试使用基于数组的jquery输出单独的点击函数

javascript - 如何将用户输入存储到数组中?

javascript - 垂直折叠树形图

css - 链接到 <symbol> 时无法更改 SVG <use> 图标大小

javascript - 动态导入所有子文件夹导出

javascript - typescript 错误 "Property ' 值'在类型 'HTMLElement' 上不存在”, "Property ' onclick'在类型 'Element' 上不存在”