动态修改svg内部attr属性
根据页面dom中svg的id获取当前svg的document,拿到虚拟dom之后通过setAttribute修改vmDom参数
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
}
}Last updated