简体   繁体   English

Javascript无法在Windows上运行IE 8

[英]Javascript not working IE 8 on Windows

I have 4 images that I use as a navigation menu, when I click on one it lights up (changes image) and the current goes out, and so on so forth. 我有4张图像用作导航菜单,当我单击其中一张时,它会点亮(更改图像),并且电流消失,依此类推。

It works well in chrome and ff (no firebug errors) 在chrome和ff中效果很好(无萤火虫错误)

But in IE8 the functioning of the clicks (where it changes the view of a div) work it just doesn't change the img src here's the code: 但是在IE8中,点击功能(改变了div的视图)起作用了,只是它并没有改变img src,这是代码:

<li id="bulletli1">
<a href="#"> 
    <img id="bullethover1" src="img/bulleto.png" height="30px" width="30px" style="position:absolute">
<img id="bullet1" name="bullet1" height="30px" width="30px" src="img/bulletwhite.png" onmousedown="this.src='img/bulletwhite.png';document.images['bullet2'].src='img/bullet.png';document.images['bullet3'].src='img/bullet.png';document.images['bullet4'].src='img/bullet.png'" style="opacity:0.4;filter:alpha(opacity=40)"/> 
    </a></li> 

So basically what happens is inside onmousedown this.src gets set to the white bullet and all the others get set to the dark bullet point. 因此,基本上发生在onmousedown内的this.src设置为白色项目符号,所有其他项目设置为黑色项目符号。 There are no errors in the developer's tools. 开发人员的工具中没有错误。

Does this.src not work in IE8? 请问this.src在IE8中不起作用吗? Any advice would help, Thanks! 任何建议都会有所帮助,谢谢!

You shouldn't be embedding your JS into your code like this. 您不应该像这样将JS嵌入代码中。 While I advise using a library like jQuery (which will make your life easier), I'll explain without it. 虽然我建议使用类似jQuery的库(这将使您的生活更轻松),但我会在没有它的情况下进行解释。

Don't embed your JS into your code. 不要将您的JS嵌入代码中。 If you really really need to, have it call a function like this: 如果确实需要,请调用以下函数:

<img id="bullet1" name="bullet1" height="30px" width="30px" src="img/bulletwhite.png" onmousedown="bulletClicked()" style="opacity:0.4;filter:alpha(opacity=40)"/>

Then in your head section between script tags you'll run your javascript: 然后在脚本标签之间的头部,您将运行JavaScript:

function bulletClicked() {
   this.src='img/bulletwhite.png';
   document.images['bullet2'].src='img/bullet.png';
   document.images['bullet3'].src='img/bullet.png';
   document.images['bullet4'].src='img/bullet.png';
}

From what it looks like, you're going about this the wrong way, you're probably putting that onclikc code into every bullet image, slightly modified for each one. 从外观上看,您将以错误的方式进行操作,您可能会将onclikc代码放入每个项目符号图像中,并对每个项目符号稍作修改。 Instead, if you just used events you would simplify so much. 相反,如果您仅使用事件,则可以简化很多事情。

If you did something like this... (and specified your height, width, and other CSS in a style section, where they belong, don't do what you did, ever again). 如果你做了这样的事情......(并且在样式部分,在那里他们属于指定的高度,宽度和其他CSS,不这样做,你做了什么, 以后再)。

<img id="bullet1" name="bullet1" src="img/bulletwhite.png" onmousedown="bulletClicked(this)"/>

Then your javascript could be... 然后您的JavaScript可能是...

function bulletClicked(e) {
   document.images['bullet1'].src='img/bullet.png';
   document.images['bullet2'].src='img/bullet.png';
   document.images['bullet3'].src='img/bullet.png';
   document.images['bullet4'].src='img/bullet.png';
   e.src='img/bulletwhite.png';
}

There are much better ways to deal with this sort of problem, and I would highly reccomend you pick up jQuery and do some work with separating your HTML, JavaScript and CSS components of your pages. 有更好的方法来解决此类问题,我强烈建议您选择jQuery,并做一些工作来分离页面的HTML,JavaScript和CSS组件。

I cannot reproduce the described behavior. 我无法重现所描述的行为。 The images seem to get replaced OK. 图像似乎已替换好。 Any further details you can provide? 您可以提供更多详细信息吗? What happens when you click on the other 3 images? 当您单击其他3张图像时会发生什么? Do they get their image URLs straight? 他们是否直接获得图片网址?

please check out, if there doesn't exist more than 1 image with the same name/id-Attribute. 请检查是否存在不超过1张具有相同名称/ ID属性的图像。

In that case, IE would take the last of the images with the same name(note that document.images['somename'] can be an Array ), while other UserAgents will take the first One. 在这种情况下,IE将使用相同名称的最后一个图像(请注意document.images ['somename']可以是Array),而其他UserAgent将使用一个。 Maybe in that case you only don't see the change, for example if the changed image is outside the viewport. 也许在那种情况下,您只会看不到更改,例如,如果更改的图像在视口之外。

greets 打招呼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM