我getElementsByClassName
用来资助多个元素并改变它们的风格,但我发现getElementsByClassName
它不兼容跨浏览器。你能帮我简化这段代码吗?我更喜欢原生 JS 实现,但 jQuery 可能会更好,所以它也很好......
那么我怎样才能循环遍历元素并根据各个类更改某些样式....
var ele = document.getElementsByClassName("dhSliderMobile");
for (var i = 0; i < ele.length; i++) {
if (ele[i].style.display === "block") {
ele[i].style.display = "none";
var div = document.getElementsByClassName("dhImageInfoDiv");
var div1 = document.getElementsByClassName("dhSeriesInfoDiv");
for (var j = 0; j < div.length; j++) {
div[j].style.right = 0 + "px";
div1[j].style.right = 0 + "px";
}
}
else {
ele[i].style.display = "block";
var div = document.getElementsByClassName("dhImageInfoDiv");
var div1 = document.getElementsByClassName("dhSeriesInfoDiv");
for (var j = 0; j < div.length; j++) {
div[j].style.right = 40 + "px";
div1[j].style.right = 40 + "px";
}
}
}
var ele = document.getElementsByClassName("dhSliderDesktop");
for (var i = 0; i < ele.length; i++) {
if (ele[i].style.display === "block") {
ele[i].style.display = "none";
var div = document.getElementsByClassName("dhImageInfoDiv");
var div1 = document.getElementsByClassName("dhSeriesInfoDiv");
for (var j = 0; j < div.length; j++) {
div[j].style.right = 0 + "px";
div1[j].style.right = 0 + "px";
}
}
else {
ele[i].style.display = "block";
var div = document.getElementsByClassName("dhImageInfoDiv");
var div1 = document.getElementsByClassName("dhSeriesInfoDiv");
for (var j = 0; j < div.length; j++) {
div[j].style.right = 20 + "px";
div1[j].style.right = 20 + "px";
}
}
}