Javascript切换图像边框在Firefox中有效,但在IE中无效
我下面的代码可以在Firefox中切换图像的边框,但在IE中不起作用。你知道为什么吗?在IE中,第一次单击将添加边框,但第二次单击不会再次删除边框 谢谢, 莱斯利Javascript切换图像边框在Firefox中有效,但在IE中无效,javascript,internet-explorer,firefox,compatibility,Javascript,Internet Explorer,Firefox,Compatibility,我下面的代码可以在Firefox中切换图像的边框,但在IE中不起作用。你知道为什么吗?在IE中,第一次单击将添加边框,但第二次单击不会再次删除边框 谢谢, 莱斯利 window.onload=function(){ 对于(var i=0;i
window.onload=function(){
对于(var i=0;i
注意返回的差异。这可能只是一个IE问题-你应该使用类赋值而不是字符串比较
for(var i = 0; i < document.images.length; i++)
{
alert(this.style.border);
document.images[i].onclick=function(){
if(this.style.border == '2px solid red')
{
this.style.border='';
}
else
{ // missed this bracket AND shorthand for borders should be
this.style.border='2px solid red';
}
}
}
for(var i=0;i
您已经使用了if(this.style.border=='2px纯红')
,它适用于FF,但
IE返回红色2px固体
,因此如果条件与IE不匹配,您可以尝试此操作
window.onload=function(){
for(var i = 0; i < document.images.length; i++)
{
document.images[i].onclick=function(){
if(this.style.borderColor == 'red' && this.style.borderStyle=='solid' && this.style.borderWidth=='2px')
{
this.style.border='none';
}
else this.style.border='2px solid red';
}
}
}
window.onload=function(){
对于(var i=0;i
备选方案:或(当您有其他类时会更好)。这没关系,因为else语句在一行上。用真实问题进行了修改-不管怎样,整洁的代码总是令人愉快的。看起来像是xbrowser问题。奇怪的是,当我试图使用
color
属性做一些事情时,我遇到了相反的问题。IE返回了我设置的#800000
,但是Firefox返回了rgb(128,0,0)
。有时候,我希望浏览器能吐出他们得到的,并为getComputedStyle()
留下计算值,但是。。。
window.onload=function(){
for(var i = 0; i < document.images.length; i++)
{
document.images[i].onclick=function(){
if(this.style.borderColor == 'red' && this.style.borderStyle=='solid' && this.style.borderWidth=='2px')
{
this.style.border='none';
}
else this.style.border='2px solid red';
}
}
}