IMG SRC tags and JavaScript

前端 未结 13 1221
眼角桃花
眼角桃花 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:24

    I've had to do something like this before, and IIRC the trick winds up being that you can't change an src attribute of an image that's part of the DOM tree. So your best bet is to write your HTML skeleton without the image and 1)create an onLoad function that generates a new img element with document.createElement, 2) set the src attribute with setAttribute(), and 3) attach it to your DOM tree.

提交回复
热议问题