无法运行js文件

时间:2017-02-24 22:44:30

标签: javascript html

我正在尝试使用html和jscript创建一个二十一点游戏,这就是我在html中加载de js文件时发生的事情我知道它已加载因为调试器告诉我的文件已经被激活但文件没有运行。 新到js

这是文件

<!DOCTYPE html>
<html>
<head>
  <title>BlackJack by RuiVieira</title>
</head>
<body>
  <div id="zonaBanca">
    <h1>Banca</h1>
  </div>
  <div id="zonaJogoBanca">
  </div>
  <div id="zonaJogador">    
    <h1>Jogador</h1>
  </div>
  <div id="zonaJogoJogador">    
  </div>
  <div id="zonaAposta">
    <h2>Aposta</h2>
    <button id="subir">+1</button>
    <button id="parar">stop</button>
    <div>
        <input id="valorAposta" type="number" min="0" step="1"/>
    </div>
    <div>
        <button id="botaoAposta">Apostar</button>
    </div>
  </div>
  <script src="blackj.js"></script>
</body>
</html>

和javascript文件:

alert('xupasaoquadrado');
var baralho =    ['Ao','2o','3o','4o','5o','6o','7o','8o','9o','10o','Vo','Do','Ko', 'Ae','2e','3e','4e','5e','6e','7e','8e','9e','10e','Ve','De','Ke',
        'Ap','2p','3p','4p','5p','6p','7p','8p','9p','10p','Vp','Dp','Kp', 'Ac','2c','3c','4c','5c','6c','7c','8c','9c','10c','Vc','Dc','Kc'];
var botaoAposta = document.getElementById('botaoAposta');
var valorAposta;
botaoAposta.onclick = function() {
alert('xupas');
valorAposta = document.getElementById('valorAposta').value;
alert('xupas1');
if(valorAposta = document.getElementById('valorAposta').value != null){

}
else{
    alert('iserir valor de aposta (números inteiros)');
}
}

3 个答案:

答案 0 :(得分:0)

将您的javascript代码包装在load事件处理程序

window.addEventListener("load", loaded, false);

function loaded() {
  alert('xupasaoquadrado');
  var baralho = ['Ao', '2o', '3o', '4o', '5o', '6o', '7o', '8o', '9o', '10o', 'Vo', 'Do', 'Ko', 'Ae', '2e', '3e', '4e', '5e', '6e', '7e', '8e', '9e', '10e', 'Ve', 'De', 'Ke', 'Ap', '2p', '3p', '4p', '5p', '6p', '7p', '8p', '9p', '10p', 'Vp', 'Dp', 'Kp', 'Ac', '2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', '10c', 'Vc', 'Dc', 'Kc'];
  var botaoAposta = document.getElementById('botaoAposta');
  var valorAposta;
  botaoAposta.onclick = function() {
    alert('xupas');
    valorAposta = document.getElementById('valorAposta').value;
    alert('xupas1');
    if (valorAposta = document.getElementById('valorAposta').value != null) {

    } else {
      alert('iserir valor de aposta (números inteiros)');
    }
  }
}

这样,当页面打开时,它将在页面加载时执行代码(意味着已经创建了DOM并且所有元素都在页面上)。

然后,当javascript代码运行时,它将遇到所有document.getElementById()语句并在DOM中找到相应的元素。

答案 1 :(得分:0)

将您的代码包含在 DOMContentLoaded 事件中,当初始HTML文档已完全加载并解析时会触发此事件:

 document.addEventListener("DOMContentLoaded", function(event) {
    alert('xupasaoquadrado');
    var baralho = ['Ao','2o','3o','4o','5o','6o','7o','8o','9o','10o','Vo','Do','Ko', 'Ae','2e','3e','4e','5e','6e','7e','8e','9e','10e','Ve','De','Ke','Ap','2p','3p','4p','5p','6p','7p','8p','9p','10p','Vp','Dp','Kp', 'Ac','2c','3c','4c','5c','6c','7c','8c','9c','10c','Vc','Dc','Kc'];
    var botaoAposta = document.getElementById('botaoAposta');
    var valorAposta; 
    var botaoAposta.onclick = function() {
        alert('xupas');
        valorAposta = document.getElementById('valorAposta').value;
        alert('xupas1');

        if(valorAposta = document.getElementById('valorAposta').value != null){

        }
        else{
            alert('iserir valor de aposta (números inteiros)');
        }
    }

});

答案 2 :(得分:0)

检查jsfiddle中的代码我看到错误,而是使用onclick绑定事件使用addEventListener在对象准备就绪时向事件添加处理程序。

   alert('xupasaoquadrado');
    var baralho = ['Ao','2o','3o','4o','5o','6o','7o','8o','9o','10o','Vo','Do','Ko', 'Ae','2e','3e','4e','5e','6e','7e','8e','9e','10e','Ve','De','Ke','Ap','2p','3p','4p','5p','6p','7p','8p','9p','10p','Vp','Dp','Kp', 'Ac','2c','3c','4c','5c','6c','7c','8c','9c','10c','Vc','Dc','Kc'];
    var botaoAposta = document.getElementById('botaoAposta');
    var valorAposta; 
    botaoAposta.addEventListener("click",function(e){
   alert('xupas');
        valorAposta = document.getElementById('valorAposta').value;
        alert('xupas1');

        if(valorAposta = document.getElementById('valorAposta').value != null){

        }
        else{
            alert('iserir valor de aposta (números inteiros)');
        }
},false);