要改变路径属性'd',可以使用以下解决方法,而不使用getElementBy...的反模式:
// 选择路径元素
const pathElement = document.querySelector('path');
// 获取当前路径属性值
const currentPath = pathElement.getAttribute('d');
// 修改路径属性值
const newPath = 'M10 10 L50 50';
pathElement.setAttribute('d', newPath);
// 给路径元素添加id属性
//
// 选择路径元素
const pathElement = document.getElementById('myPath');
// 获取当前路径属性值
const currentPath = pathElement.getAttribute('d');
// 修改路径属性值
const newPath = 'M10 10 L50 50';
pathElement.setAttribute('d', newPath);
这些方法都避免了使用getElementBy...系列方法,而是使用更灵活的querySelector和getElementById来选择元素,并使用getAttribute和setAttribute来获取和设置路径属性。