错误:无法在Img字段中将属性'src'设置为null

时间:2020-06-05 09:57:14

标签: javascript html

我正在使用JavaScript创建图像滑块。

我将图片网址放入数组中,并使用 javascript函数将图片幻灯片化。

@JsonProperty
var imageurls = ["https://s3-ap-southeast-1.amazonaws.com/he-public-data/Avengers3c9095c4.jpg",
"https://s3-ap-southeast-1.amazonaws.com/he-public-data/Avengers1dde2dff.jpg", 
"https://s3-ap-southeast-1.amazonaws.com/he-public-data/Avengers2f856ebb.jpg.", 
"https://s3-ap-southeast-1.amazonaws.com/he-public-data/Avengers40b07bc7.jpg",
"https://s3-ap-southeast-1.amazonaws.com/he-public-data/Avengers51fd2ccc.jpg"];

var index=0;
document.getElementById("demo").src = imageurls[index];

function nextSlider(){
    index++;
    if(index>4){
      index=4;
    }
    document.getElementById("demo").src = imageurls[index];
}

function prevSlider() {
    index--;
    if(index<0){
        index=0;
    }
    document.getElementById("demo").src = imageurls[index];
}

现在我收到此错误:

enter image description here

main.js:4 未捕获的TypeError: 无法将属性“ src”设置为null 在main.js:4

我不知道如何解决?还是我在哪里做错了?

2 个答案:

答案 0 :(得分:1)

在末尾之前添加javascript文件。标签



CREATE OR REPLACE FUNCTION acc_is_exclusive(acc_no integer, acc_type char) RETURNS boolean AS $$
    BEGIN
        RETURN EXISTS (SELECT FROM account WHERE account_no = acc_no AND account_type = acc_type);
    END;
$$ LANGUAGE plpgsql;
      var imageurls = [
        'https://s3-ap-southeast-1.amazonaws.com/he-public-data/Avengers3c9095c4.jpg',
        'https://s3-ap-southeast-1.amazonaws.com/he-public-data/Avengers1dde2dff.jpg',
        'https://s3-ap-southeast-1.amazonaws.com/he-public-data/Avengers2f856ebb.jpg.',
        'https://s3-ap-southeast-1.amazonaws.com/he-public-data/Avengers40b07bc7.jpg',
        'https://s3-ap-southeast-1.amazonaws.com/he-public-data/Avengers51fd2ccc.jpg'
      ]

      var index = 0
      document.getElementById('demo').src = imageurls[index]

      function nextSlider () {
        index++
        if (index > 4) {
          index = 4
        }
        document.getElementById('demo').src = imageurls[index]
      }

      function prevSlider () {
        index--
        if (index < 0) {
          index = 0
        }
        document.getElementById('demo').src = imageurls[index]
      }

答案 1 :(得分:1)

我已将您的问题摘录为摘要,它非常完美。 因此,可能在加载javascript元素之前已加载html。 在这种情况下,您javascript找不到元素eand,所以它是src

解决方案

javascript放在<body>末尾之前,就像这样:

<!DOCTYPE html>
<html lang="en">
  <body>
    <!-- your html...-->
    <script src="yourJS.js"></script>
  </body>
</html>