简体   繁体   English

如何将自定义集数据传递给 src 图像“onClick”?

[英]How can I pass custom set data to a src image "onClick"?

I have this function我有这个功能

 function changeImage() {
        console.log(event.target.getAttribute('data-image-path'));
        document.getElementById('image-id').src = event.target.getAttribute('data-image-path');
    }

on click I want to pass the data-image-path into the changeImage function, the data-image-path is inside the li so is the onclick:单击时我想将data-image-path传递到changeImage函数中, data-image-pathli内部,onclick 也是如此:

<li data-image-path="./assets/img/my_imgs/features/sc2.png" onclick='changeImage(this.data-image-path)'> 
<div class="inside-li">
<img src="./assets/img/my_imgs/features/feat1.png" alt="image"> 
<p>Easy Scheduling &amp; Attendance Tracking</p>
</div>
<div class="plan-hr">
   <hr class="hr-1">
   <hr class="hr-2">
</div>
</li>

image:图片: 李像

I have multiple li elements and each contains a different src image inside its data-image-path but how can I pass the data?我有多个li元素,每个元素在其data-image-path都包含一个不同的src图像,但如何传递数据?

This is the img element that should recieve the src :这是应该接收srcimg元素:

<div class="image-loader col-md-6 col-sm-12">
    <div class="banner-thumb-wrap">
       <div class="banner-thumb">
          <img class="header-inner-img" id="image-id">
       </div>
    </div>
</div>

Here is the result in my console.log and code:这是我的 console.log 和代码中的结果: 在此处输入图片说明 在此处输入图片说明

Use event.target.getAttribute('data-image-path') to get the clicked li 's data-image-path value inside changeImage() method.使用event.target.getAttribute('data-image-path')changeImage()方法中获取点击的lidata-image-path值。

 function changeImage() { console.log(event.target.getAttribute('data-image-path')); document.getElementById('image-id').src = event.target.getAttribute('data-image-path'); }
 <img class="header-inner-img" id="image-id"> <li data-image-path="./assets/img/my_imgs/features/sc2.png" onclick='changeImage()'> </li>

You can change your HTML into this:您可以将 HTML 更改为:

<li data-image-path="./assets/img/my_imgs/features/sc2.png" onclick='changeImage(event.target.dataset.imagePath)'> </li>

But there is a better version:但是有一个更好的版本:

Select the li elements with querySelectorAll and addEventListener for them afterward.选择li的元素querySelectorAlladdEventListener后来他们。

Try this, pass this to changeImage function and get the attribute of the clicked element.试试这个,将this传递给changeImage函数并获取被点击元素的属性。

function changeImage(obj) {
        var imagePath2 = obj.getAttribute('data-image-path');
        document.getElementById('image-id').src = imagePath2;
    }

<li data-image-path="./assets/img/my_imgs/features/sc2.png" onclick='changeImage(this)'> </li>

I slightly changed your html code, and significantly changed the js code using the forEach method, with a click listener inside.我稍微更改了您的 html 代码,并使用forEach方法显着更改了 js 代码,其中包含一个click侦听器。 This means that calling the onclick=changeImage (this.data-image-path) event is no longer needed!这意味着不再需要调用onclick=changeImage (this.data-image-path)事件! I also left the console so that you can see which attribute is being loaded on click at the moment.我还离开了console以便您可以查看当前单击时正在加载哪个属性。

I hope that is exactly what you needed.我希望这正是你所需要的。

 var li_attr = document.querySelectorAll('li'); var img = document.getElementById('image-id'); Array.from(li_attr).forEach(function(li_attrArray, i) { li_attrArray.addEventListener('click', function() { imagePath2 = li_attrArray.getAttribute('data-image-path'); img.src = imagePath2; console.log(img.src); }); });
 <li data-image-path="./assets/img/my_imgs/features/sc1.png">1</li> <li data-image-path="./assets/img/my_imgs/features/sc2.png">2</li> <li data-image-path="./assets/img/my_imgs/features/sc3.png">3</li> <li data-image-path="./assets/img/my_imgs/features/sc4.png">4</li> <img class="header-inner-img" id="image-id">

You can pass the event and then access the data through the event.target.getAttribute()您可以传递事件,然后通过event.target.getAttribute()访问数据

 function changeImage(e) { const dataImagePath = e.target.getAttribute('data-image-path'); document.getElementById('image-id').src = dataImagePath; console.log(dataImagePath); console.log(document.getElementById('image-id').src); }
 <li data-image-path="./assets/img/my_imgs/features/sc2.png" onclick='changeImage(event)'> </li> <img class="header-inner-img" id="image-id">

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

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