![](/img/trans.png)
[英]How to select parent element by which include an img child with specific src attribute
[英]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.