简体   繁体   English

如何更改img src属性作为其父选择

[英]how to change img src attribute as it's parent select

I am using this code for my carousel, I want to change src attribute of an <img> tag base on it's parent "aria-selected" attribute set it to "true" manually. 我正在为轮播使用代码,我想基于其父"aria-selected"属性将<img>标签的src属性更改为手动设置为“ true”。

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

How can I achieve this, with javascript? 如何使用JavaScript实现呢?

If I understand correclty you want to get the image element in the current item scope and replace its src . 如果我理解正确性,则希望在当前item范围中获取image元素并替换其src So something like this should work: 所以这样的事情应该工作:

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM