计算器未在控制台 HTML/JS 上显示结果

您需要访问 HTMLCollection 的第一个元素并将该<img>元素附加到该元素。此外,首先将新创建的元素存储在变量中,以便您可以在附加之前设置属性。要在图像后添加换行符和文本,您可以附加新创建的<br>元素并用于.append添加文本。


const parent = document.getElementsByClassName('chartlyrics')[0];

parent.innerHTML = '';

var img = document.createElement("img");

img.src = "img/103-logo.jpg"

img.setAttribute('class', "")

parent.appendChild(img);

parent.appendChild(document.createElement('br'));

parent.append('Powered by ');

但是,查找具有特定类的每个单个元素以仅获取一个元素是非常浪费的。您应该使用document.querySelectorto 获取与选择器匹配的第一个元素。


const parent = document.querySelector('.chartlyrics');

parent.innerHTML = '';

var img = document.createElement("img");

img.src = "img/103-logo.jpg"

img.setAttribute('class', "")

parent.appendChild(img);

parent.appendChild(document.createElement('br'));

parent.append('Powered by ');


繁星点点滴滴
浏览 120回答 2
2回答

茅侃侃

const suma = document.getElementById('suma');const resta = document.getElementById('resta');const multiplicacion = document.getElementById('multiplicacion');const division = document.getElementById('division');const output = document.getElementById('output');const num1 = document.getElementById('num1');const num2 = document.getElementById('num2');suma.addEventListener('click', ()=>{&nbsp; output.innerHTML = `${parseFloat(num1.value) + parseFloat(num2.value)}`;});resta.addEventListener('click', ()=>{&nbsp; output.innerHTML = `${parseFloat(num1.value) - parseFloat(num2.value)}`;});multiplicacion.addEventListener('click', ()=>{&nbsp; output.innerHTML = `${parseFloat(num1.value) * parseFloat(num2.value)}`;});division.addEventListener('click', ()=>{&nbsp; output.innerHTML = `${parseFloat(num1.value) / parseFloat(num2.value)}`;});<!DOCTYPE html><html dir="ltr">&nbsp; <head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title> Calculadora </title>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; <input type = "number" id = "num1" />&nbsp; &nbsp; &nbsp; <input type = "number" id = "num2" />&nbsp; &nbsp; </p>&nbsp; &nbsp; <p> <strong> ¿Qué operación desea hacer? </strong> </p>&nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; <input type = "button" value = "Sumar" id = "suma" />&nbsp; &nbsp; &nbsp; <input type = "button" value = "Restar" id = "resta" />&nbsp; &nbsp; &nbsp; <input type = "button" value = "Multiplicar" id = "multiplicacion" />&nbsp; &nbsp; &nbsp; <input type = "button" value = "Dividir" id = "division" />&nbsp; &nbsp; </p>&nbsp; &nbsp; <p id='output' style='color: red;'></p>&nbsp; &nbsp; <script src="script.js"> </script>&nbsp; </body></html>看看document.write()

泛舟湖上清波郎朗

您的代码中存在一些问题。您没有使用click事件来调用您的函数你没有op在你的函数中定义您在函数中添加了控制台日志,这意味着您在函数中调用该函数。这是行不通的。我已经为你安排好了一切!您可以使用onClick函数将您calculation作为参数传递给您的函数operations,并检查传递的内容switch和case此外,要获取values输入的number输入,您需要使用parseInt()以便将输入strings转换为数字format。此外,您需要确保在值中输入了值,否则将显示点击功能NaN。为避免这种情况,我们可以使用isNan函数来确保有一个valuefor 计算。如果没有值,您可以只显示一条消息说请输入一个值。完整的工作演示:var results = document.getElementById('results');function operations(value) {&nbsp; var numero_1 = parseInt(document.getElementById('num1').value);&nbsp; var numero_2 = parseInt(document.getElementById('num2').value);&nbsp; if (!isNaN(numero_1) && !isNaN(numero_2)) {&nbsp; &nbsp; switch (value) {&nbsp; &nbsp; &nbsp; case 'Sumar':&nbsp; &nbsp; &nbsp; &nbsp; results.innerHTML = 'Results: ' + (numero_1 + numero_2);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 'Restar':&nbsp; &nbsp; &nbsp; &nbsp; results.innerHTML = 'Results: ' + (numero_1 - numero_2);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 'Multiplicar':&nbsp; &nbsp; &nbsp; &nbsp; results.innerHTML = 'Results: ' + (numero_1 * numero_2);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 'Dividir':&nbsp; &nbsp; &nbsp; &nbsp; results.innerHTML = 'Results: ' + (numero_1 / numero_2);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; var final = document.getElementById('results').textContent;&nbsp; &nbsp; console.log(final)&nbsp; } else {&nbsp; &nbsp; results.innerHTML = 'Please enter value to calculate'&nbsp; }}<!DOCTYPE html><html dir="ltr"><head>&nbsp; <meta charset="utf-8">&nbsp; <title> Calculadora </title></head><body>&nbsp; <p>&nbsp; &nbsp; <input type="number" id="num1" required />&nbsp; &nbsp; <input type="number" id="num2" required/>&nbsp; </p>&nbsp; <p> <strong> ¿Qué operación desea hacer? </strong> </p>&nbsp; <p>&nbsp; &nbsp; <input type="button" onclick="operations(this.value)" value="Sumar" id="suma" />&nbsp; &nbsp; <input type="button" onclick="operations(this.value)" value="Restar" id="resta" />&nbsp; &nbsp; <input type="button" onclick="operations(this.value)" value="Multiplicar" id="multiplicacion" />&nbsp; &nbsp; <input type="button" onclick="operations(this.value)" value="Dividir" id="division" />&nbsp; </p>&nbsp; <div id="results"></div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript