题目描述
vue js里如何封装这个函数
let heightc = [];
let heightd = [];
let isOlc = true;
let isOld = true;
clickLic(index){
this.changes('.olc',isOlc,index,heightc)
},
clickLid(index){
this.changes('.old',isOld,index,heightd)
},
changes(olc,istrue,index,heights){
let ol = document.querySelectorAll(olc);
if (istrue) {
for(var i = 0;i<ol.length; i++){
ol[i].style.height = 'auto';
heights.push(getComputedStyle(ol[i]).height);
ol[i].style.height = '0px';
}
istrue = false
}
if (ol[index].style.height == '0px') {
ol[index].style.height = heights[index]
}else{
ol[index].style.height = 0
}
},
问题是每次点击执行时 istrue 还是 true 并没有变为 false 导致每次执行这个true 并且后面也不会触发 else条件
看了下函数参数,布尔值是不能进行修改的。 把 istrue 换成 if (heights == '') {....} 就行了