【发布时间】:2020-05-22 06:51:28
【问题描述】:
今天我正在研究幻灯片功能。我刚刚创建了一个循环,我想在循环中设置一个元素的样式,但是出错了。
slideshow.js:
import React from 'react';
let slideIndex = [1, 1];
let slideId = ["slide"];
showSlides(1, 0);
showSlides(1, 1);
function plusSlides(n, no) {
showSlides(slideIndex[no] += n, no);
}
function showSlides(n, no) {
let i;
let x = document.getElementsByClassName(slideId[no]);
if (n > x.length) {
slideIndex[no] = 1
}
if (n < 1) {
slideIndex[no] = x.length
}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex[no]-1].style.display = "block";
}
const SlideShow = (props) => {
return (
<div className={'slide'} style={{
display: 'none',
textAlign: 'center'
}}>
<img src={props.logo} className={'logo-banner'} style={{
verticalAlign: 'center'
}}/>
<a className={"prev-photo"} onClick={plusSlides(-1, 1)}>❮</a>
<a className={"next-photo"} onClick={plusSlides(1, 1)}>❯</a>
</div>
)
};
export default SlideShow;
我的问题出现在第 22 行和第 24 行。因为我给了它一个 style.display,但这不起作用...有其他人更好的解决方案。请告诉我!
【问题讨论】:
标签: javascript css reactjs syntax jsx