如何通过innerHTML设置图像 [英] How to set image via 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
andadv
in the name of your image URL. Adblockers will block these images or addinline
styling to yourimg
attributes containing that image.
希望有帮助!
这篇关于如何通过innerHTML设置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!