Javascript 在Firefox上更改颜色,但不在IE或Chrome上更改颜色
我很难弄明白为什么这在chrome或IE上不起作用,但在firefox上却很有效。本质上,我有一个导航栏,我高亮显示了被点击的链接,直到另一个链接被点击 以下是我的JS函数:Javascript 在Firefox上更改颜色,但不在IE或Chrome上更改颜色,javascript,html,internet-explorer,google-chrome,cross-browser,Javascript,Html,Internet Explorer,Google Chrome,Cross Browser,我很难弄明白为什么这在chrome或IE上不起作用,但在firefox上却很有效。本质上,我有一个导航栏,我高亮显示了被点击的链接,直到另一个链接被点击 以下是我的JS函数: function updateObjectIframe(which){ document.getElementById('one').innerHTML = '<'+'object id="foo" style="height:100%;width:100%;" name="foo" type="text/h
function updateObjectIframe(which){
document.getElementById('one').innerHTML = '<'+'object id="foo" style="height:100%;width:100%;" name="foo" type="text/html" data="'+which.href+'"><\/object>';
var Elements = document.getElementsByClassName('button');
Array.prototype.filter.call(Elements, function(Element){
Element.style = "background: #F0E68C;color: black;";
});
which.style = "background: #333; color: #fff;";
}
函数updateObjectFrame(哪个){
document.getElementById('one')。innerHTML='';
var Elements=document.getElementsByClassName('button');
Array.prototype.filter.call(元素,函数(元素){
Element.style=“背景:#F0E68C;颜色:黑色;”;
});
which.style=“背景:#333;颜色:#fff;”;
}
我的导航条如下所示。页面被加载到id为“一”的文件中
任何帮助都将不胜感激 在javascript中更改样式时,需要设置样式的各个组件。像这样:
...
Element.style.background = "#F0E68C";
Element.style.color = "black";
...
which.style.background = "#333"
which.style.color = "#fff";
...
我会尝试使用普通的JavaScript技术重写它。如果你避开任何聪明的事情,你总是最安全的。以下是Chrome和IE9中的预期(或我推断的预期)视觉效果。您的奇怪循环构造在这两种情况下都起作用,但传统的
for
循环更为清晰。失败的是将字符串指定给样式属性。这看起来是一个不可移植的Firefox扩展
function updateObjectIframe(which){
document.getElementById('one').innerHTML = '<'+'object id="foo" style="height:100%;width:100%;" name="foo" type="text/html" data="'+which.href+'"><\/object>';
var elements = document.getElementsByClassName('button');
for ( var i = 0; i < elements.length; ++i ) {
var element = elements[i];
element.style.background = '#F0E68C'
element.style.color = 'black';
}
which.style.background = '#333';
which.style.color = '#fff';
}
用这样的话:
onlick="$(".classWhichAllYourLinksHave").style("background","WhatEverYouUse")this.style = "background: #F0E68C;color: black;""
而不是
onclick="updateObjectIframe(this); return false;"
或者调用一个函数关闭
并不是真的关闭(缺少/
)-可能是这样吗?不,很抱歉,当我将它转移到这里时,这是一个输入错误:(是的,这就是为什么我要检查该数组中的每个元素,并在每个元素中设置背景和颜色。然后我将刚刚单击的元素设置为不同的颜色。非常棒,效果非常好!谢谢你,不客气!但是请看我关于IE旧版本的更新。我很高兴你摆姿势说我最终是这样做的,并且只是登录发布它。)你自己!
onlick="$(".classWhichAllYourLinksHave").style("background","WhatEverYouUse")this.style = "background: #F0E68C;color: black;""
onclick="updateObjectIframe(this); return false;"