简体   繁体   中英

Change image src onmouseover div

I want to change src of img when onmouseover that div-12. How can I achieve that?

<div class="row">
 <div class="col-md-12" style="margin-left: 40px; margin-top: 5px;">
    <a href="{{ action('') }}" id="link-settings" href="">
     <div id="block-settings" class="col-xs-2 button-left-premium">
        <img class="button-img" src="{{ asset('/img.png') }}"></img>
      </div>
     <div class="col-xs-5 button-right-premium" id="block-settings-two">
     <h3 class="settings-title" style="margin-top: 10px; text-align: center; font-weight: bold;">test</h3>
   </div>
 </a>

const div = document.querySelector('your-div');
const img = document.querySelector('your-img');
div.onmouseover = () => img.src = 'your src';

This should work.

Via JQuery, we add a listener to the hover event of the col-md-12 div and change the src attribute to the image

 $('.col-md-12').hover(function() { original = $('.button-img').attr('src'); $('.button-img').attr('src', 'img2.jpg'); }, function() { $('.button-img').attr('src', original); })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row"> <div class="col-md-12" style="margin-left: 40px; margin-top: 5px;"> <a href="{{ action('') }}" id="link-settings" href=""> <div id="block-settings" class="col-xs-2 button-left-premium"> <img class="button-img" src="{{ asset('/img.png') }}"></img> </div> <div class="col-xs-5 button-right-premium" id="block-settings-two"> <h3 class="settings-title" style="margin-top: 10px; text-align: center; font-weight: bold;">test</h3> </div> </a>

Using plain JS:

let a = document.getElementById('a');
let b = document.getElementById('b');

a.addEventListener('mouseover', function () {
    let newSrc = '/b.jpg';

    if (a.getAttribute('src') !== newSrc) {
        a.src = newSrc;
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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