我正在尝试为元素设置属性。
该元素从一个类(活动)中提取并分配一个变量。从不同的类 (topArrow) 中提取相同的元素并分配一个变量。
在比较两个变量以运行 if 语句时,评估结果不一样......???
console.log 看看发生了什么,我得到了这个:
HTMLCollection[img.active images/a.../top.png]
HTMLCollection[]
在没有 topArrow 类的元素上,我得到了这个:
HTMLCollection[img.active images/a...left.png]
HTMLCollection[img.topArrow images/a.../top.png]
var arrow = 0;
var topArrow = document.getElementsByClassName("topArrow");
var menuText = document.getElementsByClassName("menuText");
var rightArrow = document.getElementsByClassName("rightArrow");
var bottomArrow = document.getElementsByClassName("bottomArrow");
var leftArrow = document.getElementsByClassName("leftArrow");
//assign navButtons to var buttons (creates array)
var buttons = document.getElementsByClassName("navButton");
//for each instance of buttons, assign class "active" onmouseover
for(var i = 0; i < buttons.length; ++i){
buttons[i].onmouseover = function() {
this.className = "active";
var arrow = document.getElementsByClassName("active");
console.log(arrow);
console.log(topArrow);
changeImages();
}
}
//for each instance of buttons, remove class "active" onmouseout
for(var i = 0; i < buttons.length; ++i){
buttons[i].onmouseout = function () {
this.className = "";
};
}
function changeImages(){
if ( arrow == topArrow ) {
this.setAttribute("src", "images/arrows/top_o.png");
console.log("arrow should change");
} else {
console.log("arrow should not change");
}
}