javascript - OnError not shown the default image - Stack Overflow

I'm trying to display a default image when the other specified in src doesn't exists, so I di

I'm trying to display a default image when the other specified in src doesn't exists, so I did:

<img src=".././uploads/images/avatar/' + user.id + '.png" onerror="this.src='+"assets/img/default-avatar.png"+'"></div>';

the problem is that I still get the broken thumbnail. Strange thing is that: if I switch the src, so I place assets/img/default-avatar.png in the src all works well, any idea?

I'm trying to display a default image when the other specified in src doesn't exists, so I did:

<img src=".././uploads/images/avatar/' + user.id + '.png" onerror="this.src='+"assets/img/default-avatar.png"+'"></div>';

the problem is that I still get the broken thumbnail. Strange thing is that: if I switch the src, so I place assets/img/default-avatar.png in the src all works well, any idea?

Share Improve this question asked Feb 12, 2019 at 12:01 sfarzososfarzoso 1,6407 gold badges31 silver badges83 bronze badges 4
  • 1 what these + sign means? – Maheer Ali Commented Feb 12, 2019 at 12:02
  • 1 @MaheerAli concatenate the string path – sfarzoso Commented Feb 12, 2019 at 12:02
  • Are you writing this in a JS file? – Nick Parsons Commented Feb 12, 2019 at 12:03
  • @NickParsons yes, I need to concatenate it because it start with ' – sfarzoso Commented Feb 12, 2019 at 12:04
Add a ment  | 

2 Answers 2

Reset to default 3

Make sure that quotes matching:

'<img src=".././uploads/images/avatar/' + user.id + '.png" onerror="this.src='+"assets/img/default-avatar.png"+'"></div>';

Or, more readable, use the power of template strings:

`<img src=".././uploads/images/avatar/${user.id}.png" onerror="this.src='${"assets/img/default-avatar.png"}'"></div>`;

Put like this,

onerror="javascript:this.src='/assets/img/default-avatar.png'"

OR

onerror="this.src='/assets/img/default-avatar.png'"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745575274a4633924.html

相关推荐

  • javascript - OnError not shown the default image - Stack Overflow

    I'm trying to display a default image when the other specified in src doesn't exists, so I di

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信