这段代码怎么优化下啊!!
disType(nv) { let allfx = document.querySelectorAll('.fx')
let allyh = document.querySelectorAll('.yh')
let allzy = document.querySelectorAll('.zy')
if (nv == 'fx') {
this.showType = 1
allfx.forEach((fx) => {
fx.style.display = 'block'
})
allyh.forEach((yh) => {
yh.style.display = 'none'
})
allzy.forEach((zy) => {
zy.style.display = 'none'
})
} else if (nv == 'yh') {
this.showType = 2
allfx.forEach((fx) => {
fx.style.display = 'none'
})
allyh.forEach((yh) => {
yh.style.display = 'block'
})
allzy.forEach((zy) => {
zy.style.display = 'none'
})
} else if (nv == 'zy') {
this.showType = 3
allfx.forEach((fx) => {
fx.style.display = 'none'
})
allyh.forEach((yh) => {
yh.style.display = 'none'
})
allzy.forEach((zy) => {
zy.style.display = 'block'
})
}
},
回答:
disType(nv) { let clses = ['fx', 'yh', 'zy']
clses.forEach((cls, i) => {
document.querySelectorAll(`.${cls}`).forEach(ele => {
ele.style.display = nv == cls ? 'block' : 'none'
})
})
this.showType = clses.indexOf(nv) + 1
},
以上是 这段代码怎么优化下啊!! 的全部内容, 来源链接: utcz.com/p/935417.html