如何让按钮进行数学运算,然后使用 JS 显示它?

基本上,我试图让“左转”按钮显示“左转”,每次用户单击按钮时方向将减少 10(例如:第一次单击 = 170,第二次单击 = 160,第三次click = 150 等),它将显示在浏览器中。我还想制作一个“右转”按钮来做同样的事情,除了它将 10 添加到显示的最后一个方向。此外,这最初并没有发生,但是当我检查浏览器中显示 HTML 中的“车辆”未定义的元素时,我现在看到一个错误。知道那里发生了什么吗?


这是HTML:


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">


<head>

    <meta charset="utf-8" />

    <title>My Vehicles</title>

</head>


<body>


<div id="input">


<br/>

<br/>

<br/>

    <input type="text" id="engstart"> Pick Your Starting Speed</input>

    <br/>

    <button onclick="startEngine()"> Start the Car </button>

<br/>

<br/>

    <button onclick="accel()"> Accelerate </button>

<br>

<br/>

    <button onclick="carInBrake()"> Put Car In Brake </button>

<br/>

<br/>

    <button onclick="left()"> Turn Left </button>


</div>

<div id="displays">



    <script src="vehicle.js">

    </script>


    <script>


        let pickColor = prompt("Pick a color");

        //let num = prompt("Pick your starting speed in mph");

        let num = document.getElementById('engstart').value;

        let speed = parseInt(num);


        let car = new Vehicle(pickColor, 0 , speed);



        function startEngine()

        {

            alert(car.engineOn());

            document.getElementById('displays').innerHTML += car.show() + "<br>";

        }


        function accel()

        {

            alert(car.accelerate());

            document.getElementById('displays').innerHTML += car.accelerate();

        }


        function carInBrake()

        {


            alert(car.brake());


        }



    </script>

</div>



</body>


</html>


噜噜哒
浏览 132回答 2
2回答

ibeautiful

为了访问Vehicle另一个js文件中的类,您首先需要导出该类,然后在您的js文件中导入。script.jsimport { Vehicle } from './vehicle.js';let pickColor = prompt("Pick a color");//let num = prompt("Pick your starting speed in mph");let num = document.getElementById('engstart').value;let speed = parseInt(num);let car = new Vehicle(pickColor, 0, speed);window.startEngine =function() {&nbsp; alert(car.engineOn());&nbsp; document.getElementById('displays').innerHTML += car.show() + "<br>";}window.accel = function() {&nbsp; alert(car.accelerate());&nbsp; document.getElementById('displays').innerHTML += car.accelerate();}window.carInBrake = function () {&nbsp; alert(car.brake());}vehicle.jsexport class Vehicle {&nbsp; constructor(color, direction, currentSpeed, topSpeed, engineStarted) {&nbsp; &nbsp; this._color = color;&nbsp; &nbsp; this._direction = direction;&nbsp; &nbsp; this._currentSpeed = currentSpeed;&nbsp; &nbsp; this._topSpeed = 300;&nbsp; &nbsp; this._engineStarted = true;&nbsp; }&nbsp; engineOn() {&nbsp; &nbsp; this._engineStarted = true;&nbsp; &nbsp; const started = `${this._color} car engine has started (VROOM VROOM!!!)`;&nbsp; &nbsp; return started;&nbsp; }&nbsp; show() {&nbsp; &nbsp; if (this._engineStarted) {&nbsp; &nbsp; &nbsp; const start = `Starting Speed: ${this._currentSpeed} mph<br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Direction: ${this._direction}º `;&nbsp; &nbsp; &nbsp; return start;&nbsp; &nbsp; }&nbsp; }&nbsp; brake() {&nbsp; &nbsp; this._engineStarted = false;&nbsp; &nbsp; const message = "Car In Brake";&nbsp; &nbsp; return message;&nbsp; }&nbsp; accelerate() {&nbsp; &nbsp; if (this._engineStarted) {&nbsp; &nbsp; &nbsp; this._currentSpeed + 10;&nbsp; &nbsp; &nbsp; const current = `Accelerating. Current speed is now ${this._currentSpeed} mph.`;&nbsp; &nbsp; &nbsp; return current;&nbsp; &nbsp; }&nbsp; }&nbsp; left() {&nbsp; &nbsp; let dir = this._direction;&nbsp; &nbsp; let turn = dir - 10;&nbsp; &nbsp; return `Turning left. direction is now ${turn} degrees`;&nbsp; }}index.html<div id="input">&nbsp; &nbsp; <br />&nbsp; &nbsp; <br />&nbsp; &nbsp; <br />&nbsp; &nbsp; <input type="text" id="engstart"> Pick Your Starting Speed</input>&nbsp; &nbsp; <br />&nbsp; &nbsp; <button onclick="startEngine()"> Start the Car </button>&nbsp; &nbsp; <br />&nbsp; &nbsp; <br />&nbsp; &nbsp; <button onclick="accel()"> Accelerate </button>&nbsp; &nbsp; <br>&nbsp; &nbsp; <br />&nbsp; &nbsp; <button onclick="carInBrake()"> Put Car In Brake </button>&nbsp; &nbsp; <br />&nbsp; &nbsp; <br />&nbsp; &nbsp; <button onclick="left()"> Turn Left </button>&nbsp; </div>&nbsp; <div id="displays"></div>&nbsp; <script type="module" src="./script.js"></script>

白猪掌柜的

问题是left()方法中的最后一条语句。因为this._direction是类变量并且已经定义,所以你不应该在let那里使用关键字。只是改变-let&nbsp;this._direction&nbsp;=&nbsp;newTurn;到this._direction&nbsp;=&nbsp;newTurn;正如 Patrick 所指出的,return 语句也应该写在函数的末尾。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript