如何将 IMG SRC 属性更改为父选择



我将此代码用于旋转木马,我想根据<img>标记的父"aria-selected"属性手动更改其src属性并将其设置为"true"。

$(".item").on('click',function(event){
$(this).parent('a').attr("aria-selected","true")
}

如何使用javascript实现这一点?

如果我正确理解,您希望获得当前item作用域中的图像元素并替换其src。所以像这样的东西应该起作用:

links.forEach(function(item) {
item.setAttribute("aria-selected", "false");
var image = item.querySelector('img');
var currentSrc = image.getAttribute('src');
var newSrc = currentSrc.replace('.png', '-active.png');
image.setAttribute('src', newSrc);
});

最新更新