Javascript W3滑块-未捕获类型错误:无法读取属性';类名';未定义的
我的控制台中有一条失败消息。它说:Javascript W3滑块-未捕获类型错误:无法读取属性';类名';未定义的,javascript,slider,typeerror,Javascript,Slider,Typeerror,我的控制台中有一条失败消息。它说: script.js:151 Uncaught TypeError: Cannot read property 'className' of undefined at showSlides (script.js:151) at script.js:126 这是关于我从W3学校得到的一个滑块() 我对W3中的代码做了一些修改,它在控制台中显示了这条消息。此外,如果我单击滑块中的一个箭头,这将在控制台中显示: Uncaught TypeError:
script.js:151 Uncaught TypeError: Cannot read property 'className' of undefined
at showSlides (script.js:151)
at script.js:126
这是关于我从W3学校得到的一个滑块()
我对W3中的代码做了一些修改,它在控制台中显示了这条消息。此外,如果我单击滑块中的一个箭头,这将在控制台中显示:
Uncaught TypeError: dots[(slideIndex - 1)] is undefined
showSlides http://127.0.0.1:57616/js/script.js:151
<anonymous> http://127.0.0.1:57616/js/script.js:126
script.js:151:5
和JS:
// SLIDER
var slideIndex = 1;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}
//滑块
var slideIndex=1;
放映幻灯片(幻灯片索引);
//下一个/上一个控件
函数加滑块(n){
放映幻灯片(幻灯片索引+=n);
}
//缩略图图像控件
功能当前幻灯片(n){
放映幻灯片(幻灯片索引=n);
}
功能演示幻灯片(n){
var i;
var slides=document.getElementsByClassName(“mySlides”);
var dots=document.getElementsByClassName(“dot”);
如果(n>slides.length){slideIndex=1}
如果(n<1){slideIndex=slides.length}
对于(i=0;i
找不到带有var dots=document.getElementsByCassName(“dot”)的元素代码>
当尝试点[slideIndex-1]时,className+=“活动”代码>
它给出错误,无法读取未定义的属性“className”
可能是你弄乱了html中dot
类的某些元素
我在html中添加了一些dot
类的div,现在可以正常工作了
//滑块
var slideIndex=1;
放映幻灯片(幻灯片索引);
//下一个/上一个控件
函数加滑块(n){
放映幻灯片(幻灯片索引+=n);
}
//缩略图图像控件
功能当前幻灯片(n){
放映幻灯片(幻灯片索引=n);
}
功能演示幻灯片(n){
var i;
var slides=document.getElementsByClassName(“mySlides”);
//控制台日志(幻灯片);
var dots=document.getElementsByClassName(“dot”);
如果(n>slides.length){slideIndex=1}
如果(n<1){slideIndex=slides.length}
对于(i=0;i
.slideshow容器{
z指数:20;
最大宽度:400px;
位置:绝对位置;
保证金:自动;
最高:45%;
左:45%;
背景图片:url(/img/szene2 slider BG.png);
背景重复:无重复;
背景来源:填充框;
背景位置:中心;
背景大小:100%;}
/*默认情况下隐藏图像*/
迈斯利德斯先生{
显示:无;
}
/*下一步和上一步按钮*/
.prev,.next{
光标:指针;
位置:绝对位置;
最高:50%;
宽度:自动;
利润上限:-22px;
填充:16px;
颜色:白色;
字体大小:粗体;
字号:18px;
过渡:0.6s缓解;
边界半径:50%;
用户选择:无;
背景色:#c7113c;
}
/*将“下一步按钮”置于右侧*/
.下一个{
右:0;
边界半径:50%;
}
/*在悬停时,添加一个黑色背景色,带有一点透明*/
.prev:悬停,.next:悬停{
背景色:#333333;
}
.标题文字{
颜色:#F2F2;
字体大小:15px;
填充:8px 12px;
位置:绝对位置;
底部:-10px;
宽度:100%;
文本对齐:居中;
}
/*点/子弹/指示器*/
多特先生{
光标:指针;
高度:150像素;
宽度:150px;
边际:0.2px;
背景色:#bbb;
边界半径:50%;
显示:内联块;
过渡:背景色0.6s;
}
.active,.dot:悬停{
背景色:#717171;
}
/*褪色动画*/
.褪色{
-webkit动画名称:淡入淡出;
-webkit动画持续时间:1.5s;
动画名称:淡入淡出;
动画持续时间:1.5s;
}
@-webkit关键帧淡入淡出{
来自{opacity:.4}
到{opacity:1}
}
@关键帧淡入淡出{
来自{opacity:.4}
到{opacity:1}
}
Werkzeuge
费尔
Nutztiere
❮
❯
谢谢你的回答!成功了!
.slideshow-container {
z-index: 20;
max-width: 400px;
position: absolute;
margin: auto;
top: 45%;
left: 45%;
background-image: url(/img/szene2-slider-BG.png);
background-repeat: no-repeat;
background-origin: padding-box;
background-position: center;
background-size: 100%; }
/* Hide the images by default */
.mySlides {
display: none;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 50%;
user-select: none;
background-color: #c7113c;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 50%;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: #333333;
}
.caption-text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: -10px;
width: 100%;
text-align: center;
}
/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
// SLIDER
var slideIndex = 1;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}