Javascript 如何更改img src属性,因为它';s父选择
我正在为我的旋转木马使用代码,我想更改Javascript 如何更改img src属性,因为它';s父选择,javascript,jquery,Javascript,Jquery,我正在为我的旋转木马使用代码,我想更改标记的src属性,基于它的父属性“aria selected”属性手动将其设置为“true” $(".item").on('click',function(event){ $(this).parent('a').attr("aria-selected","true") } 使用javascript如何实现这一点?如果我理解正确,您希望在当前项中获取image元素,并替换其src。因此,类似这样的方法应该有效: links.forEach(function
标记的src
属性,基于它的父属性“aria selected”
属性手动将其设置为“true”
$(".item").on('click',function(event){
$(this).parent('a').attr("aria-selected","true")
}
使用javascript如何实现这一点?如果我理解正确,您希望在当前
项中获取image元素,并替换其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);
});
谢谢@LazerBass,我从你的建议中得到了灵感,并按照以下代码进行操作。