开发者问题收集

未捕获的 TypeError:无法将属性“innerHTML”设置为 null - Typewriter JS 代码

2020-06-23
134

我们使用以下代码作为文本的打字机效果。它之前运行良好,并将“_CONTENT”变量中设置的文本显示为文本,然后删除并输入下一行,但现在我们在控制台中收到 “未捕获的类型错误:无法将属性“innerHTML”设置为 null” ,并且没有显示任何文本。

请问有什么想法可以解决这个问题?

<script>

// List of sentences
var _CONTENT = [ 
    "Domain Name", 
    "Website Hosting Plan", 
    "Virtual Private Server", 
    "WordPress Hosting Plan",
    "Web Design Package", 
    "Reseller Hosting Plan"
];

// Current sentence being processed
var _PART = 0;

// Character number of the current sentence being processed 
var _PART_INDEX = 0;

// Holds the handle returned from setInterval
var _INTERVAL_VAL;

// Element that holds the text
var _ELEMENT = document.querySelector("#text");

// Cursor element 
var _CURSOR = document.querySelector("#cursor");

// Implements typing effect
function Type() { 
    // Get substring with 1 characater added
    var text =  _CONTENT[_PART].substring(0, _PART_INDEX + 1);
    _ELEMENT.innerHTML = text;
    _PART_INDEX++;

    // If full sentence has been displayed then start to delete the sentence after some time
    if(text === _CONTENT[_PART]) {

        clearInterval(_INTERVAL_VAL);
        setTimeout(function() {
            _INTERVAL_VAL = setInterval(Delete, 50);
        }, 2000);
    }
}

// Implements deleting effect
function Delete() {
    // Get substring with 1 characater deleted
    var text =  _CONTENT[_PART].substring(0, _PART_INDEX - 1);
    _ELEMENT.innerHTML = text;
    _PART_INDEX--;


    // If sentence has been deleted then start to display the next sentence
    if(text === '') {
        clearInterval(_INTERVAL_VAL);

        // If current sentence was last then display the first one, else move to the next
        if(_PART == (_CONTENT.length - 1))
            _PART = 0;
        else
            _PART++;
        
        _PART_INDEX = 0;

        // Start to display the next sentence after some time
        setTimeout(function() {
            _CURSOR.style.display = 'inline-block';
            _INTERVAL_VAL = setInterval(Type, 100);
        }, 200);
    }
}

// Start the typing effect on load
_INTERVAL_VAL = setInterval(Type, 100);
</script>

<div id="container">
<div id="text"></div><div id="cursor"></div>
</div>

谢谢。

2个回答

document.addEventListener('DOMContentLoaded', function(){}) 之间添加代码,以便在页面加载后执行 javascript 代码

document.addEventListener('DOMContentLoaded', function(){

// List of sentences
var _CONTENT = [ 
    "Domain Name", 
    "Website Hosting Plan", 
    "Virtual Private Server", 
    "WordPress Hosting Plan",
    "Web Design Package", 
    "Reseller Hosting Plan"
];

// Current sentence being processed
var _PART = 0;

// Character number of the current sentence being processed 
var _PART_INDEX = 0;

// Holds the handle returned from setInterval
var _INTERVAL_VAL;

// Element that holds the text
var _ELEMENT = document.querySelector("#text");

// Cursor element 
var _CURSOR = document.querySelector("#cursor");

// Implements typing effect
function Type() { 
    // Get substring with 1 characater added
    var text =  _CONTENT[_PART].substring(0, _PART_INDEX + 1);
    _ELEMENT.innerHTML = text;
    _PART_INDEX++;

    // If full sentence has been displayed then start to delete the sentence after some time
    if(text === _CONTENT[_PART]) {

        clearInterval(_INTERVAL_VAL);
        setTimeout(function() {
            _INTERVAL_VAL = setInterval(Delete, 50);
        }, 2000);
    }
}

// Implements deleting effect
function Delete() {
    // Get substring with 1 characater deleted
    var text =  _CONTENT[_PART].substring(0, _PART_INDEX - 1);
    _ELEMENT.innerHTML = text;
    _PART_INDEX--;


    // If sentence has been deleted then start to display the next sentence
    if(text === '') {
        clearInterval(_INTERVAL_VAL);

        // If current sentence was last then display the first one, else move to the next
        if(_PART == (_CONTENT.length - 1))
            _PART = 0;
        else
            _PART++;
        
        _PART_INDEX = 0;

        // Start to display the next sentence after some time
        setTimeout(function() {
            _CURSOR.style.display = 'inline-block';
            _INTERVAL_VAL = setInterval(Type, 100);
        }, 200);
    }
}

// Start the typing effect on load
_INTERVAL_VAL = setInterval(Type, 100);
})
<div id="container">
<div id="text"></div><div id="cursor"></div>
</div>
Hitesh Tripathi
2020-06-23

这是一个快速修复。

修复说明:

  1. 我已将全局变量 _ELEMENT 和 _CURSOR 移至实际使用它们的函数中。
  2. 我已使用 Vanilla JS document.getElementByID 访问两个 DOM 元素 这是您要寻找的解决方案吗?

这是代码,希望对您有所帮助!:)

<script>
  // List of sentences
  var _CONTENT = [
    "Domain Name",
    "Website Hosting Plan",
    "Virtual Private Server",
    "WordPress Hosting Plan",
    "Web Design Package",
    "Reseller Hosting Plan",
  ];

  // Current sentence being processed
  var _PART = 0;

  // Character number of the current sentence being processed
  var _PART_INDEX = 0;

  // Holds the handle returned from setInterval
  var _INTERVAL_VAL;

  // Implements typing effect
  function Type() {
    // Element that holds the text
    var _ELEMENT = document.getElementById("text");

    // Get substring with 1 characater added
    var text = _CONTENT[_PART].substring(0, _PART_INDEX + 1);
    _ELEMENT.innerHTML = text;
    _PART_INDEX++;

    // If full sentence has been displayed then start to delete the sentence after some time
    if (text === _CONTENT[_PART]) {
      clearInterval(_INTERVAL_VAL);
      setTimeout(function () {
        _INTERVAL_VAL = setInterval(Delete, 50);
      }, 2000);
    }
  }

  // Implements deleting effect
  function Delete() {
    // Element that holds the text
    var _ELEMENT = document.getElementById("text");
    // Cursor element
    var _CURSOR = document.getElementById("cursor");

    // Get substring with 1 characater deleted
    var text = _CONTENT[_PART].substring(0, _PART_INDEX - 1);
    _ELEMENT.innerHTML = text;
    _PART_INDEX--;

    // If sentence has been deleted then start to display the next sentence
    if (text === "") {
      clearInterval(_INTERVAL_VAL);

      // If current sentence was last then display the first one, else move to the next
      if (_PART == _CONTENT.length - 1) _PART = 0;
      else _PART++;

      _PART_INDEX = 0;

      // Start to display the next sentence after some time
      setTimeout(function () {
        _CURSOR.style.display = "inline-block";
        _INTERVAL_VAL = setInterval(Type, 100);
      }, 200);
    }
  }

  // Start the typing effect on load
  _INTERVAL_VAL = setInterval(Type, 100);
</script>

<div id="container">
  <div id="text"></div>
  <div id="cursor"></div>
</div>
Shahzada Fahad Ashraf
2020-06-23