通过 Javascript 设置图像源在 Internet Explorer 中不可靠
我只是想通过 JavaScript 更改图像的 SRC
属性,如下所示:
document.getElementById('fooImage').src = img;
其中 img
是一个具有文件链接的变量。
在所有其他浏览器(Chrome、Firefox、Safari)中,这都有效。在 IE (7+) 中,这也有时有效。
使用IE内置的开发者工具,我可以看到图像的SRC
标签已设置。本地窗口中是否还有其他内容可以帮助我调试为什么图像实际上没有显示在屏幕上?
我也尝试使用 jQuery 来执行此操作并获得相同的结果:
$("#fooImage").attr("src", img);
An ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在调试过程中,我会首先对其进行硬编码...
我在我的网站中使用了以下内容,它的工作原理如下...
其他一些需要检查的事情:
确保您的 ID=标签不在 部分中,而是在
部分中...例如
<< img src="你的图片" id="someid">
。如果 div 标签中包含 `id='someid' 则您无法更改图片/图片不会显示。你在使用window.onload吗?,body onload?使用第一个的正确方法是..
尝试不同的测试图像。我过去在显示 png 时遇到问题,我将其更改为 gif 或 jpg 并且它有效。我不明白这是怎么“很久以前”的,但它似乎不再是一个问题了,但是嘿......尝试一下。
尝试完整的网址
使用https?
尝试将图像粘贴到程序中的其他位置,看看会发生什么。
尝试将其添加到您的 HTML 中(将您的网站代替我的网站 - 在 google 上查找 BASE href 以获取更多信息)
确保图像没有隐藏在图层后面(我知道)在某些浏览器中有效)
告诉我们该网站,以便我们查看并获取更多信息来帮助您调试这个,查看上下文(周围的代码)帮助...
In debugging this I would hard code it first...
I've used the following in my website and it works like this...
Some other things to check:
Make sure your ID= tag is not in the
<DIV
section but inside the<IMG
section... for example<div class="style1"><img src="yourpicture" id="someid">
. If `id='someid' is in the div tag then you can't change the picture / the picture won't show up.are you using window.onload?, body onload? the proper way to use the first is..
Try a different test image. I had issues in the past with showing png's, I changed it to a gif or jpg and it worked. I don't understand how that was "way back" but it doesn't seem to be an issue anymore but hey... something to try.
try a full url
using https?
try sticking the image somewhere else in your program and see what happens.
try adding this to your HTML (put your website in place of mine - lookup BASE href on google for more info)
Make sure the image isn't hidden behind a layer (I know it works in some browsers)
tell us the website so we can check it out and get more info to help you in debugging this, seeing context (surrounding code) helps...
鉴于它在其他浏览器中工作,搜索这个主题似乎经常出现问题是 IE 如何缓存图像(参考 Epascarello 的评论)。你的代码与我的代码相同 - 除了 IE10 之外,它工作正常。
Given that it works in other browsers, searching on this topic it seems that often the problem is how IE caches images (ref. Epascarello's comment). Your code is the same as what I have - it works fine except in IE10.
我也面临这个难题。然后发现它在“页面检查器”中工作,因此经过一番挖掘后发现(在 Internet Explorer 中)通过转到 Tools.Internet Options.Advanced
取消选中“禁用脚本调试 (Internet Explorer)”及其下面的一项。
I too, faced this conundrum. Then discovered that it works in 'Page Inspector', so after some digging discovered that (in Internet Explorer) by going to Tools.Internet Options.Advanced
uncheck the 'Disable script debugging (Internet Explorer)' and the one below it.
更改 image.src 后,IE9 会出现这种情况
我发现在使用
var strVar="C:Users/x/Desktop/caution.png"
image.src=strVar
并调用警报(image.src)我会在警报框中得到类似这样的信息:
所以我尝试了
image.src=strVar.replace(/\"/g,"")
删除引号
成功了!
警报(图像.src)
I found that with IE9 after changing an image.src with
var strVar="C:Users/x/Desktop/caution.png"
image.src=strVar
and calling an alert(image.src) I would get something like this n the alertbox:
So I tried
image.src=strVar.replace(/\"/g,"")
to remove qoutemarks
and it worked!
alert(image.src)