通过 Javascript 设置图像源在 Internet Explorer 中不可靠

发布于 2024-12-13 08:06:35 字数 468 浏览 3 评论 0 原文

我只是想通过 JavaScript 更改图像的 SRC 属性,如下所示:

document.getElementById('fooImage').src = img;

其中 img 是一个具有文件链接的变量。

在所有其他浏览器(Chrome、Firefox、Safari)中,这都有效。在 IE (7+) 中,这也有时有效。

使用IE内置的开发者工具,我可以看到图像的SRC标签已设置。本地窗口中是否还有其他内容可以帮助我调试为什么图像实际上没有显示在屏幕上?

我也尝试使用 jQuery 来执行此操作并获得相同的结果:

$("#fooImage").attr("src", img);

An ideas?

I am simply trying to change the SRC attribute of an image via javascript like so:

document.getElementById('fooImage').src = img;

Where img is a variable that has a link to the file.

In all other browsers (Chrome, Firefox, Safari) this works. In IE (7+) this also works too sometimes.

Using IE's built-in developer tools, I can see that the image's SRC tag is set. Is there something else in the locals window that could help me debug why the image doesn't actually show on screen?

I've also tried using jQuery to do this and same outcome:

$("#fooImage").attr("src", img);

An ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫﹏色ふ单纯 2024-12-20 08:06:35

在调试过程中,我会首先对其进行硬编码...

document.getElementById('fooImage').src = "myimage.png";

我在我的网站中使用了以下内容,它的工作原理如下...

var imgCounter = document.getElementById('formtimer');
imgCounter.src = "graphics/odometers/1.png";

其他一些需要检查的事情:

  1. 确保您的 ID=标签不在 部分中,而是在 部分中...例如

    << img src="你的图片" id="someid">。如果 div 标签中包含 `id='someid' 则您无法更改图片/图片不会显示。

  2. 你在使用window.onload吗?,body onload?使用第一个的正确方法是..

     window.onload = function () { YourFunctionHere(); };
    
  3. 尝试不同的测试图像。我过去在显示 png 时遇到问题,我将其更改为 gif 或 jpg 并且它有效。我不明白这是怎么“很久以前”的,但它似乎不再是一个问题了,但是嘿......尝试一下。

  4. 尝试完整的网址

  5. 使用https?

  6. 尝试将图像粘贴到程序中的其他位置,看看会发生什么。

  7. 尝试将其添加到您的 HTML 中(将您的网站代替我的网站 - 在 google 上查找 BASE href 以获取更多信息)

    
    
  8. 确保图像没有隐藏在图层后面(我知道)在某些浏览器中有效)

  9. 告诉我们该网站,以便我们查看并获取更多信息来帮助您调试这个,查看上下文(周围的代码)帮助...

In debugging this I would hard code it first...

document.getElementById('fooImage').src = "myimage.png";

I've used the following in my website and it works like this...

var imgCounter = document.getElementById('formtimer');
imgCounter.src = "graphics/odometers/1.png";

Some other things to check:

  1. 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.

  2. are you using window.onload?, body onload? the proper way to use the first is..

     window.onload = function () { YourFunctionHere(); };
    
  3. 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.

  4. try a full url

  5. using https?

  6. try sticking the image somewhere else in your program and see what happens.

  7. try adding this to your HTML (put your website in place of mine - lookup BASE href on google for more info)

    <BASE href="http://perrycs/" />
    
  8. Make sure the image isn't hidden behind a layer (I know it works in some browsers)

  9. 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...

冷夜 2024-12-20 08:06:35

鉴于它在其他浏览器中工作,搜索这个主题似乎经常出现问题是 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.

橘和柠 2024-12-20 08:06:35

我也面临这个难题。然后发现它在“页面检查器”中工作,因此经过一番挖掘后发现(在 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.

病女 2024-12-20 08:06:35

更改 image.src 后,IE9 会出现这种情况

我发现在使用var strVar="C:Users/x/Desktop/caution.png"
image.src=strVar

并调用警报(image.src)我会在警报框中得到类似这样的信息:

<块引用>

文件:///C:Users/x/Desktop/"C:Users/x/Desktop/caution.png"

所以我尝试了
image.src=strVar.replace(/\"/g,"")
删除引号
成功了!
警报(图像.src)

<块引用>

文件:///C:Users/x/Desktop/caution.png

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:

file:///C:Users/x/Desktop/"C:Users/x/Desktop/caution.png"

So I tried
image.src=strVar.replace(/\"/g,"")
to remove qoutemarks
and it worked!
alert(image.src)

file:///C:Users/x/Desktop/caution.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文