【问题标题】:Cant find style of 'undefined'找不到“未定义”的样式
【发布时间】:2021-05-21 02:04:34
【问题描述】:

首先,我使用 Hugo、Webpack、Babel 和 nodeJS。 现在一切都在编译,但我得到了一些控制台错误。

我在 Chrome localhost 控制台中遇到的相关错误:

bundle.js:2 Uncaught TypeError: Cannot read property 'style' of undefined
    at bundle.js:2
    at Object.671 (bundle.js:2)
    at n (bundle.js:2)
    at Module.150 (bundle.js:2)
    at n (bundle.js:2)
    at bundle.js:2
    at bundle.js:2

我在 Firefox localhost 控制台中遇到的相关错误:

Uncaught TypeError: t[e] is undefined
    671 http://localhost:1313/assets/js/bundle.js:2
    671 http://localhost:1313/assets/js/bundle.js:2
    n http://localhost:1313/assets/js/bundle.js:2
    150 http://localhost:1313/assets/js/bundle.js:2
    n http://localhost:1313/assets/js/bundle.js:2
    <anonymous> http://localhost:1313/assets/js/bundle.js:2
    <anonymous> http://localhost:1313/assets/js/bundle.js:2

未定义的示例:

      (function (e) {
        'use strict';
        let t = document.getElementsByClassName('bstTab');
        t[e].style.display = 'block',
        0 === e ? (document.getElementById('prevBstBtn').style.display = 'none', document.getElementById('nextBstBtn').style.visibility = 'visible') : document.getElementById('prevBstBtn').style.display = 'inline',
        e === t.length - 1 ? (document.getElementById('nextBstBtn').style.visibility = 'hidden', document.getElementById('ppBstBtn').type = 'image') : (document.getElementById('nextBstBtn').innerHTML = 'Next', document.getElementById('ppBstBtn').type = 'hidden'),
        function (e) {
          let t,
          n = document.getElementsByClassName('step');
          for (t = 0; t < n.length; t++) n[t].className = n[t].className.replace(' active', '');
          n[e].className += ' active'
        }(e)
      }) (0),

这是来自 localhost 控制台链接的 bundle.js 的源代码。

进入捆绑包之前的实际功能:

function showBstTab(n) {
    //alert(n);
    // This function will display the specified tab of the form ...
    "use strict";
    let x = document.getElementsByClassName("bstTab");
    x[n].style.display = "block";
    // ... and fix the Previous/Next buttons:
    if (n === 0) {
        document.getElementById("prevBstBtn").style.display = "none";
        document.getElementById("nextBstBtn").style.visibility = 'visible';
    } else {
        document.getElementById("prevBstBtn").style.display = "inline";
    }
    if (n === (x.length - 1)) {
        document.getElementById("nextBstBtn").style.visibility = 'hidden';
        //nextBtn.type ="hidden";
        document.getElementById("ppBstBtn").type = "image";
    } else {
        document.getElementById("nextBstBtn").innerHTML = "Next";
        document.getElementById("ppBstBtn").type = "hidden";
    }
    // ... and run a function that displays the correct step indicator:
    fixBstStepIndicator(n);
}

【问题讨论】:

  • 听起来你没有找到元素
  • 对,但我该如何解决呢?
  • html 模块是否需要与它捆绑在一起?还是我需要将 bundle.js 链接到 htmls?类似的东西。现在我有从 head.html 调用的捆绑脚本,但不是 html 模块
  • 感谢工作。 @epascarello

标签: javascript node.js webpack bundle hugo


【解决方案1】:

我修复这个错误的方法是阅读这个Javascript can't find element by id?

我实际上只是将 &lt;script src="./assets/js/bundle.js"&gt;&lt;/script&gt; 从头部移到了 footer.html 的底部。

问题在于它正在尝试加载技术上尚未声明的内容,因此您将其移动到页脚,以便在调用之前声明所有内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-14
    • 2022-01-04
    • 2022-06-22
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    相关资源
    最近更新 更多