> For the complete documentation index, see [llms.txt](https://408550179s-organization.gitbook.io/blog/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://408550179s-organization.gitbook.io/blog/dong-tai-xiu-gai-svg-nei-bu-attr-shu-xing.md).

# 动态修改svg内部attr属性

```javascript
import { isArray } from 'lodash-es'
export const setSvg = (option) => {
  if (option) {
    if (typeof option == 'string') {
      option = JSON.parse(option)
    }
    if (isArray(option)) {
      setTimeout(() => {
        option.forEach((item) => {
          if (checkVal(item.id)) {
            const ids_ = item.id.replace(/#/g, '').split(' ')
            let svgEmbed = null
            let svgEmbedPath = null

            ids_.map((e, i) => {
              if (i == 0) {
                if (document.getElementById(e)) {
                  svgEmbed = document.getElementById(e).getSVGDocument()
                } else {
                  return
                }
              } else {
                if (svgEmbed) {
                  svgEmbedPath = svgEmbed.getElementById(e)
                }
              }
            })
            if (item.list) {
              item.list.forEach((c) => {
                const keys = Object.keys(c)
                const val = Object.values(c)
                keys.forEach((d, ki) => {
                  svgEmbedPath && svgEmbedPath.setAttribute(d, val[ki])
                })
              })
            }
          }
        })
      }, 100)
    }
  }
}
const checkVal = (ids) => {
  if (ids) {
    if (typeof ids === 'string') {
      return true
    } else {
      return false
    }
  } else {
    return false
  }
}
```
