首页 > 解决方案 > ReferenceError = document.querySelectorAll('[data-number]')

问题描述

const numberButtons = document.querySelectorAll('[data-number]')
const operationButtons = document.querySelectorAll('[data-operation]')
const equalsButton = document.querySelector('[data-equals]')
const deleteButton = document.querySelector('[data-delete]')
const allClearButton = document.querySelector('[data-all-clear]')
const previosOperandandTextElement = document.querySelector('[data-previous-operand]')
const currentOperandandTextElement = document.querySelector('[data-current-operand]')

s
class Calculator {

    constructor(previosOperandandTextElement, currentOperandandTextElement) {
    this.previosOperandandTextElement = previosOperandandTextElement
    this.currentOperandandTextElement = currentOperandandTextElement
    this.clear()
    }

    clear() {
        this.currentOperand = ''
        this.previousOperand = ''
        this.operation = undefined
    }

    delete() {

    }

    appendNumber(number) {
        this.currentOperand = number
    }

    chooseOperation(operation) {

    }

    compute() {

    }

    updateDisplay() {
        this.currentOperandandTextElement.innerText = this.currentOperand
    }
}



const calculator = new Calculator(previosOperandandTextElement, currentOperandandTextElement)


numberButtons.forEach(button => {
    button.addEventListener('click', () => {
        calculator.appendNumber(button.innerText)
        calculator.updateDisplay()
    })
})
*, *::before, *::after {
    box-sizing:border-box;
    font-family: Gotham Rounded, sans-serif;
    font-weight: normal;
}

body {
    padding: 0;
    margin: 0;
    background: linear-gradient(to right, blue, green)
}

.calculator-grid {
    display: grid;
    justify-content: center;
    align-content: center;
    min-height: 100vh;
    grid-template-columns: repeat(4, 100px);
    grid-template-rows: minmax(120px, auto) repeat(5,100px);
}

.calculator-grid > button {
    cursor: pointer;
    font-size: 2rem;
    border: 1px solid white;
    outline: none;
    background-color: rgba(255, 255, 255, .75);
}

.calculator-grid > button:hover {
    background-color: rgba(255, 255, 255, .9);
}

.span-two {
    grid-column: span 2;
}

.output {
    grid-column: 1 / -1;
    background-color: rgba(0, 0, 0, .75);
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    flex-direction: column;
    padding: 10px;
    word-wrap: break-word;
    word-break: break-all;
}

.output .previous-operand {
    color: rgba(255, 255, 255, .75);
    font-size: 1rem;
}
.output .current-operand {
    color: white;
    font-size: 1rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="calculator-grid">
        <div class="output">
            <div data-previous-operand class="previous-operand"></div>
            <div data-current-operand class="current-operand"></div>
        </div>
        <button data-all-clear class="span-two">AC</button>
        <button data-delete>DEL</button>
        <button data-operation>/</button>
        <button data number>1</button>
        <button data number>2</button>
        <button data number>3</button>
        <button data-operation>*</button>
        <button data number>4</button>
        <button data number>5</button>
        <button data number>6</button>
        <button data-operation>+</button>
        <button data number>7</button>
        <button data number>8</button>
        <button data number>9</button>
        <button data-operation>-</button>
        <button data number>.</button>
        <button data number>0</button>
        <button data-equals class="span-two">=</button>
    </div>
    <script src="script.js" defer></script>
</body>
</html>

我正在使用 HTML、CSS 和 JS 构建我的第一个 javascript 计算器项目。当我在实时服务器中打开它时,我的 JS 代码没有任何反应。当我运行此代码是 VS Code 时,我注意到输出控制台中有一个参考错误。我已经切换了我的脚本标签,但什么也没有。关于这里可能是什么问题的任何帮助?这是带有错误的控制台的图像

标签: javascriptreferenceerror

解决方案


Node.js 不提供document对象。浏览器可以。

如果您的程序设计为从<script>HTML 文档中的元素运行,那么从那里运行它。不要使用 Node.js 运行它。


推荐阅读