【问题标题】:Adding attributes to a generated SVG file向生成的 SVG 文件添加属性
【发布时间】:2016-11-23 13:33:44
【问题描述】:

在我的项目中,我使用 grunt 根据单个 svg 文件/图形生成 svg sprite 表。为此,我使用了名为“grunt-svg-sprite”的 grunt 贡献。

我的第一个问题是:是否可以将属性(例如 ID 或 preserveAspectRation 属性)添加到生成的 sprite 的根元素中? grunt-svg-sprite 是否附带执行此类操作的选项?阅读文档对我没有帮助。

另外,我尝试使用 grunt-execute 将任务添加到我的 gruntfile。此任务将在生成 svg sprite 后立即运行。这个想法是调用外部 javascript 来插入属性,类似于:

Gruntfile.js

execute: {
        target: {
            src: ['svgsprite_script.js']
        }
    }

svgsprite_script.js

var root = document.documentElement;
root.setAttribute('preserveAspectRatio', 'none');

我应该以某种方式指定要修改的文件吗?上面的代码抛出一个关于“文档”未定义的错误。

如果有办法,将属性添加到生成的 svg 文件的最佳方法是什么?或者更准确地说:如何将“perserveAspectRatio”属性添加到使用“grunt-svg-sprite”生成的 svg sprite?

提前致谢。

【问题讨论】:

    标签: javascript svg gruntjs


    【解决方案1】:

    您可以使用cheerio 来加载您的svgsprite 文件。它为您提供了一个类似于 jquery 的文档界面,您可以使用它以多种方式更改文档...包括添加 ID

    https://github.com/cheeriojs/cheerio

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 2013-09-22
      • 2010-10-07
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多