我需要更改我页面上所有 svg 的 url。我有以下代码,但是当我尝试使用 setAttributeNS 时出现错误。
jQuery("[*|href*='icons.svg']:not([href])").each(function () {
hashedIconUrl = jQuery(this).attr('xlink:href').replace('icons.svg', hashedIcon);
console.log(hashedIconUrl);
jQuery(this).setAttributeNS('http://www.w3.org/1999/xlink', 'href', hashedIconUrl);
});
最佳答案
你需要从jQuery对象转换为原生DOM对象来调用setAttributeNS。通常的方法是通过 [0]
jQuery("[*|href*='icons.svg']:not([href])").each(function () {
hashedIconUrl = jQuery(this).attr('xlink:href').replace('icons.svg', hashedIcon);
console.log(hashedIconUrl);
jQuery(this)[0].setAttributeNS('http://www.w3.org/1999/xlink', 'href', hashedIconUrl);
});
关于javascript - 在每个循环中使用 setAttributeNS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56001796/