IMG SRC tags and JavaScript

前端 未结 13 1145
眼角桃花
眼角桃花 2020-12-02 02:09

Is it possible to call a JavaScript function from the IMG SRC tag to get an image url?

Like this:





        
相关标签:
13条回答
  • 2020-12-02 02:27

    how about this?

    var imgsBlocks = new Array( '/1.png', '/2.png', '/3.png');
    function getImageUrl(elemid) {  
    var ind = document.getElementById(elemid).selectedIndex;
    document.getElementById("get_img").src=imgsBlocks[ind]; 
    }
    

    it's not work?

    <img src="'+imgsBlocks[2]+'" id="get_img"/>
    
    0 讨论(0)
  • 2020-12-02 02:28

    No. The Img's SRC attribute is not an event, therefore the inline JS will never fire.

    0 讨论(0)
  • 2020-12-02 02:29

    you could dynamically feed the image by calling an aspx page in the SRC.

    Ex;

    <img src="provideImage.aspx?someparameter=x" />
    

    On the page side, you`ll need to put the image in the response and change the content type for an image.

    The only "problem" is that your images won't be indexed a you better put some cache on that provider page or you'll ravage the server.

    0 讨论(0)
  • 2020-12-02 02:30

    OnLoad event of image called again and again do some thing like this

    0 讨论(0)
  • 2020-12-02 02:31

    If you're in the mood for hacks, this works as well.

    <img src='blah' onerror="this.src='url to image'">
    
    0 讨论(0)
  • 2020-12-02 02:35

    Nope. It's not possible, at least not in all browsers. You can do something like this instead:

    <img src="blank.png" id="image" alt="just nothing">
    <script type="text/javascript">
        document.getElementById('image').src = "yourpicture.png";
    </script>
    

    Your favourite JavaScript framework will provide nicer ways :)

    0 讨论(0)
提交回复
热议问题