admin管理员组文章数量:1432006
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?
-
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
2 Answers
Reset to default 3Make 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'"
本文标签: javascriptOnError not shown the default imageStack Overflow
版权声明:本文标题:javascript - OnError not shown the default image - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745575274a2664290.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论