Javascript图像src单击更改事件
我正在做一个图像切换器,我正在尝试应用在那里的解决方案,但我不能控制自己 非常简单的事情,我正在尝试做一个图像切换器。单击后团队成员正在更改,成员名称的类别也将从“活动成员”链接更改为“成员”链接。不幸的是,我不知道为什么两者都不起作用,请与我分享你的想法。我想在将来增加更多的成员,这样就不会出现if语句 请给我香草的Javascript图像src单击更改事件,javascript,css,image,Javascript,Css,Image,我正在做一个图像切换器,我正在尝试应用在那里的解决方案,但我不能控制自己 非常简单的事情,我正在尝试做一个图像切换器。单击后团队成员正在更改,成员名称的类别也将从“活动成员”链接更改为“成员”链接。不幸的是,我不知道为什么两者都不起作用,请与我分享你的想法。我想在将来增加更多的成员,这样就不会出现if语句 请给我香草的 <script> function changeImage(img) { let image = docume
<script>
function changeImage(img) {
let image = document.getElementById("ChangePic");
let currentAddres = document.getElementById("ChangePic").src;
image.src = img.src.replace("$currentAddres", "{{$site_url}img/{$this.innerHTML.jpg}");
let currentActive = document.getElementsByClass("active-members-link");
currentActive.class = "members-links";
this.class = "active-members-link";
}
</script>
谢谢您用于引用元素。以下是引用元素的三种方式:
文本的文本
[{url:*,txt:*},{…},{…},{…},]
*/
让数据=[{
网址:'https://styles.redditmedia.com/t5_3g6gs7/styles/profileIcon_6gz6l1b77jd61.jpg?width=256&height=256&crop=256:256,smart&s=36d1caa14aa1bf9f67a6c35fb1d588b31613459b',
txt:‘忍者女孩’
}, {
网址:'https://steamuserimages-a.akamaihd.net/ugc/871873319171427467/DF62F343D27001BCD92704A77C6472FD5451E7BD/',
txt:“阿祖米”
}, {
网址:'https://store.playstation.com/store/api/chihiro/00_09_000/container/US/en/19/UP4312-CUSA02383_00-AV00000000000001/image?w=320&h=320&bg_color=000000&opacity=100&_version=00_09_000',
txt:“狙击手”
}];
//引用的父元素
身材{
边框:3件镶灰色;
宽度:150px;
高度:150像素;
光标:指针;
}
img{
宽度:100%;
}
菲卡普顿{
字体:2ch/1控制台;
文本对齐:居中;
填充物:3px2px;
}
单击图像或文本
您是否打算使用而不是document.getElementsByClass和currentActive.class?不幸的是,此更改无效;
<section class="fp-section margin-div">
<div class="fp-main-visual">
<img class="fp-main-visual__image fp-main-visual__image--pc ofi" src="{$site_url}img/big3.jpg" width="300" height="300" alt="{$cl_dat.cl_name}">
<img class="fp-main-visual__image fp-main-visual__image--sp ofi" id="changePic" src="{$site_url}img/John John.jpg" width="300" height="300" alt="{$cl_dat.cl_name}">
</div>
<div class="container after-article mx-auto">
<div class="row">
<div class="col-12 col-lg-6 text-center"><a class="active-members-link" href="" onclick="changeImage(this)">John John</a></div>
<div class="col-12 col-lg-6 text-center"><a class="members-links" href="" onclick="changeImage(this)">John2 John2</a></div>
</div>
</div>
</section>