如何通过innerHTML设置图像 [英] How to set image via innerHTML

查看:86
本文介绍了如何通过innerHTML设置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令人困惑的问题...

Confusing question...

在回显中,我有空白页..如果我设置innerHTML ="some text";- 有用.为什么它不能处理图像?如果我直接转到 https://domain.com/adv/banner.jpg ,它将打开图片...

In echo i have blank page.. if i set innerHTML="some text"; - it works. Why it's not working with image? If i directly goes to https://domain.com/adv/banner.jpg it will open image...

echo'
  <div id="yabanner"></div>

  <script>
  yaGetBanner();
  function yaGetBanner()
  {
    var el = document.getElementById("yabanner");
    el.innerHTML="<img src=\'https://domain.com/adv/banner.jpg\' width=\'400px\' height=\'150px\'>";
  }
  </script>
  ';

推荐答案

您还需要 img 属性的双引号,然后需要反斜杠.因此您的代码如下所示:

You need double quotes for the img attribute too, and then you need the backslashes. So your code would look like:

echo '
<div id="yabanner"></div>

<script>
yaGetBanner();
function yaGetBanner()
{
   var el = document.getElementById("yabanner");
   el.innerHTML="<img src=\"http://placehold.it/350x350\" width=\"400px\" height=\"150px\">";
}
</script>';

还请注意,图像URL的名称中有 banner adv .Adblockers将阻止这些图像或将 inline 样式添加到包含该图像的 img 属性.

Also note that you have banner and adv in the name of your image URL. Adblockers will block these images or add inline styling to your img attributes containing that image.

希望有帮助!

这篇关于如何通过innerHTML设置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆