未捕获的TypeError:无法读取属性' style'未定义的HTML

时间:2016-10-03 10:54:49

标签: javascript html css

我已经在我的页面中为幻灯片创建了一个部分!我也为它编写了HTML,CSS和Javascript,但是当我在chrome中运行它时,我得到了这个javascript错误:

未捕获的TypeError:无法读取属性' style'未定义的

showSlides @ slideshow.js:24

(匿名函数)@ slideshow.js:2

幻灯片显示框架正常工作且可见,但幻灯片显示的第一张图像未自动显示。我必须单击前进或后退按钮才能实际查看图片!!我猜它是因为这个javascript错误

任何人都可以帮我解决这个Javascript错误吗?

以下是我的HTML,CSS和Javascript文件的代码:

HTML部分:

const coinA = 2
const coinB = 5
const sum = 13

var arr = [];
arr[0] =0;
console.log(getMyTip(coinA, coinB, sum));
function getMyTip(){
    for(var i=1; i<= sum; i++){
        var minA, minB;
        if( i < coinA){
          minA = coinA - i;
        }else{
          minA = arr[i - coinA];
        }
        if( i < coinB){
         minB = coinB - i;
        }else{
         minB = arr [i - coinB]
        }
        arr[i] = Math.min(minA, minB);
    }
    return arr[sum];

}

CSS部分:

<div class="slideShow" style="height: 700px;">
<script type="text/javascript" src="../Data/Scripts/Java/slideshow.js"></script>
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_01.jpg">
</center>
<div class="text">Caption Text</div>
</div>

<div class="mySlides fade">
<div class="numbertext">2 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_02.jpg">
</center>
<div class="text">Caption Two</div>
</div>

<div class="mySlides fade">
<div class="numbertext">3 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_03.jpg">
</center>
<div class="text">Caption Three</div>
</div>

<div class="mySlides fade">
<div class="numbertext">4 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_04.jpg">
</center>
<div class="text">Caption Four</div>
</div>

<div class="mySlides fade">
<div class="numbertext">5 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_05.jpg">
</center>
<div class="text">Caption Five</div>
</div>

<div class="mySlides fade">
<div class="numbertext">6 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_06.jpg">
</center>
<div class="text">Caption Six</div>
</div>

<div class="mySlides fade">
<div class="numbertext">7 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_07.jpg">
</center>
<div class="text">Caption Seven</div>
</div>

<div class="mySlides fade">
<div class="numbertext">8 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_08.jpg">
</center>
<div class="text">Caption Eight</div>
</div>

<div class="mySlides fade">
<div class="numbertext">9 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_09.png">
</center>
<div class="text">Caption Nine</div>
</div>

<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>
<br>

<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span> 
<span class="dot" onclick="currentSlide(2)"></span> 
<span class="dot" onclick="currentSlide(3)"></span>
<span class="dot" onclick="currentSlide(4)"></span> 
<span class="dot" onclick="currentSlide(5)"></span> 
<span class="dot" onclick="currentSlide(6)"></span> 
<span class="dot" onclick="currentSlide(7)"></span> 
<span class="dot" onclick="currentSlide(8)"></span> 
<span class="dot" onclick="currentSlide(9)"></span> 
</div>
</div>

我的slideshow.js Javascript文件据说有错误,如Chrome所示

* {box-sizing:border-box}
body {font-family: Verdana,sans-serif;margin:0}
.mySlides {display:none}

/* Slideshow container */
.slideshow-container {
  max-width: 1010px;
  min-height: 500px;
  position: relative;
  margin-top: 50px;
  margin-bottom: auto;
  margin-left: auto;
  margin-right: auto;
}

/* 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: 0 3px 3px 0;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  cursor:pointer;
  height: 13px;
  width: 13px;
  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}
}

Chrome说我在第一行有错误。 2&amp; 24,但我不明白错误是什么? 有人可以帮我修复此错误吗?我们很乐意接受任何帮助:)

1 个答案:

答案 0 :(得分:0)

我实际上没有找到解决方案,但我的主要问题是,幻灯片的所有图像都应该在页面加载时自动加载,而不是使用上一个和下一个按钮!!

我发现在加载页面时我的函数没有自动加载,需要调用加载它们,例如使用上一个和下一个按钮。

所以我用

修复了它
<body onLoad="showSlides(); viewSlides();">

onLoad属性从一开始就加载我的函数,所有图像都会在此之后自动加载。

我仍然无法解决这个问题&#34; Uncaught TypeError:无法读取属性&#39; style&#39;未定义&#34; javascript错误,但我的幻灯片和他们的脚本运行正常,所以我满意它

感谢所有试图帮助我的人,不过我的问题却有所不同,并且有不同的解决方案。