Image doesn’t appears in the browser javascript.

huangapple go评论62阅读模式
英文:

Image doesn't appears in the browser javascript

问题

我正在使用HTML、CSS和JavaScript进行一个项目,我希望在单击"Yes"按钮后出现图像"noite.png",并且之后计数器必须等于0(我需要这个计数器来知道答对问题的次数)。

以下是你提供的HTML代码(我将保持原文):

<!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>Frango - O Jogo</title>
    <link rel="stylesheet" href="estilo.css">
</head>
<body>
    <h1>Title</h1>
    <input id='btn_No' type="button" value="No" onclick="click_No()">
    <input id='btn_Yes' type="button" value="Yes" onclick="click_Yes()">
    <div id="res"></div>
    <img id="img" src="" alt="">
    <script src="script.js"></script>
</body>
</html>

以及以下的CSS代码:

body{
    background-color: black;
    color: white;
    text-align: center;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14pt;
}
input{
    margin: 10px 10px;
    font-size: larger;
    padding: 8px 20px;
    border-radius: 10px 10px;
}

#btn_No{
    background-color: rgb(255, 0, 0);
    border-color: rgb(253, 10, 10);
}

#btn_Yes{
    background-color: rgb(42, 226, 5);
    border-color: rgb(42, 226, 5);
}

在你提供的JavaScript中,你有一个错误,你应该将JavaScript代码放在<script>标签中,而不是将其放在<script src="script.js"></script>标签中。请修改如下:

var res = document.getElementById('res');
var contador = 0;
var img = document.getElementById('img');

function click_No() {
    res.innerHTML = `Que pena, tente de novo!`;
    contador++;
}

function click_Yes() {
    res.innerHTML = `Acertou! Levaste ${contador} vezes para acertar!`;
    img.src = 'noite.png'; // 去掉括号
    contador = 0;
}

然后,确保你的noite.png文件位于与HTML文件相同的目录中。这样,当你点击"Yes"按钮时,图像应该会在浏览器中显示,并且计数器将重置为0。

英文:

I'm doing a project with HTML, CSS and `` and I pretend after the Yes button is clicked, should appear the image noite.png and after that the counter must be equal to 0 (I need this counter to know how many times the person needed to answer correctly the question).

The following HTML:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Frango - O Jogo&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;estilo.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Title&lt;/h1&gt;
    &lt;input id=&#39;btn_No&#39; type=&quot;button&quot; value=&quot;No&quot; onclick=&quot;click_No()&quot;&gt;
    &lt;input id=&#39;btn_Yes&#39; type=&quot;button&quot; value=&quot;Yes&quot; onclick=&quot;click_Yes()&quot;&gt;
    &lt;div id=&quot;res&quot;&gt;&lt;/div&gt;
    &lt;img id=&quot;img&quot; src=&quot;&quot; alt=&quot;&quot;&gt;
    &lt;script src=&quot;script.js&quot;&gt;
    var res = document.getElementById(&#39;res&#39;)
var contador = 0
var img = document.getElementById(&#39;img&#39;)

function click_No(){
    res.innerHTML = `Que pena, tente de novo!`
    contador++
}

    function click_Yes(){
    res.innerHTML = `Acertou! Levaste ${contador}vezes para acertar!`
    img.src = (&#39;noite.png&#39;)
    contador = 0
    }
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

And the following CSS:

body{
    background-color: black;
    color: white;
    text-align: center;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14pt;
}
input{
    margin: 10px 10px;
    font-size: larger;
    padding: 8px 20px;
    border-radius: 10px 10px;
}

#btn_No{
    background-color: rgb(255, 0, 0);
    border-color: rgb(253, 10, 10);
}

#btn_Yes{
    background-color: rgb(42, 226, 5);
    border-color: rgb(42, 226, 5);
}

Here's the output: Output

But the image doesn't appear in the browser and the counter and the counter isn't equal to 0. I think the js isn't running the lines 12 and 13 of the script. Could someone help me?

答案1

得分: 1

只需更改其中一个为 click_No

英文:

It appears that you have two click_Yes functions, of which the former is overridden by the latter. Simply change one of them to click_No.

答案2

得分: 0

  1. JavaScript 应该在 <head> 中使用 defer 属性或放在 <body> 的最后,否则它将无法选择 <script> 标签后的元素!尝试使用 console.log(img)
  2. 命名约定和现代做法,例如:驼峰命名法,使用 letconst
英文:

InSync is correct

  1. JavaScript should be in head with defer or last thing in body, other ways it will not select elements after script tag ! try console.log(img)
  2. Naming conventions and modern practices, eg: camelCase, use let and const.

huangapple
  • 本文由 发表于 2023年3月4日 01:42:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定