Changing the image source using jQuery

前端 未结 16 2056
天命终不由人
天命终不由人 2020-11-22 01:24

My DOM looks like this:

16条回答
  •  长情又很酷
    2020-11-22 02:22

    There is no way of changing the image source with CSS.

    Only possible way is using Javascript or any Javascript library like jQuery.

    Logic-

    The images are inside a div and there are no class or id with that image.

    So logic will be select the elements inside the div where the images are located.

    Then select all the images elements with loop and change the image src with Javascript / jQuery.

    Example Code with demo output-

    $(document).ready(function()
    {
        $("button").click(function()
        {
          $("#d1 .c1 a").each(function()
          {
              $(this).children('img').attr('src', 'https://www.gravatar.com/avatar/e56672acdbce5d9eda58a178ade59ffe');
          });
        });
    });
    
    
    

提交回复
热议问题