簡體   English   中英

如何更改img src屬性作為其父選擇

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

我正在為輪播使用代碼,我想基於其父"aria-selected"屬性將<img>標簽的src屬性更改為手動設置為“ true”。

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

如何使用JavaScript實現呢?

如果我理解正確性,則希望在當前item范圍中獲取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);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM