jQuery get the image src

前端 未结 6 860
醉梦人生
醉梦人生 2020-12-04 16:29

I hope when I click the button, I can get the specific img src and show the img src in the div class img-block block.

HTML



        
相关标签:
6条回答
  • 2020-12-04 16:36

    src should be in quotes:

    $('.img1 img').attr('src');
    
    0 讨论(0)
  • 2020-12-04 16:39

    for full url use

    $('#imageContainerId').prop('src')
    

    for relative image url use

    $('#imageContainerId').attr('src')
    

    function showImgUrl(){
      console.log('for full image url ' + $('#imageId').prop('src') );
      console.log('for relative image url ' + $('#imageId').attr('src'));
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <img id='imageId' src='images/image1.jpg' height='50px' width='50px'/>
    
    <input type='button' onclick='showImgUrl()' value='click to see the url of the img' />

    0 讨论(0)
  • 2020-12-04 16:42

    In my case this format worked on latest version of jQuery:

    $('img#post_image_preview').src;
    
    0 讨论(0)
  • 2020-12-04 16:43

    When dealing with the HTML DOM (ie. this), the array selector [0] must be used to retrieve the jQuery element from the Javascript array.

    $(this)[0].getAttribute('src');
    
    0 讨论(0)
  • 2020-12-04 16:48

    This is what you need

     $('img').context.currentSrc
    
    0 讨论(0)
  • 2020-12-04 16:54

    You may find likr

    $('.class').find('tag').attr('src');
    
    0 讨论(0)
提交回复
热议问题