-6

我一直在尝试使 JSBin 中的所有 Javascript 页面代码在单击按钮时自动工作。问题包括无法运行代码,因为它说我的脚本中有多个变量不能一起工作,并且由于 console.log 不起作用,因此无法将它们全部放在 HTML 中。我尝试了几个不同的想法,但遗憾的是,我无法正确地做到这一点。

我的代码是:

var name = prompt('So what is your name?');

var confirmName = confirm('So your name is ' + UCFL(name) + '?');

function UCFL(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}

if (confirmName === true) {
var start = confirm('Good. Lets start the roleplay, Sir ' + UCFL(name) + '. Are you  
ready?');
}
if (confirmName === false) {
var name = prompt('Than what is your name?');
var confirmNamed = confirm('So your name is ' + UCFL(name) + '?');
}

if (confirmNamed === true) {
var start = confirm('Good. Lets start the roleplay, Sir ' + UCFL(name) + '. Are you    
ready?');
}
if (confirmNamed === false) {
var name = prompt('Than what is your name?');
var confirmName = confirm('So your name is ' + UCFL(name) + '?');
if (confirmName === true) {
var start = confirm('Good. Lets start the roleplay, Sir ' + UCFL(name) + '. Are you
ready?');
}
if (confirmName === false) {
alert('Oh, guess what? I do not even fucking care what your name is anymore. Lets just 
start..');
var start = confirm('Are you ready?');
}
}

if (start === true) {
var x = console.log(Math.floor(Math.random() * 5));
if (x === 1) {
    alert('You are an dwarf in a time of great  disease.');
    alert('');
}

}

这就是我希望你解决的问题:

<!DOCTYPE html>
<html>

<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>

<body>
<form>
<input type="button" value="Start The Game" onclick="" />
</form>
</body>

</html>
4

2 回答 2

0

将您的代码放在一个函数中,例如:

<!DOCTYPE html>
<html>

<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<script>
    function runGame() {
        // put your js code here
    }
</script>
</head>

<body>
<form>
<input type="button" value="Start The Game" onclick="runGame();" />
</form>
</body>

</html>

将您的 js 代码复制到另一个文件并使用脚本标记导入它也是一个好主意,例如:

<script src="path/to/file.js"></script>
于 2013-05-07T23:36:24.277 回答
0

我在 JSBin 上创建了一个条目,建议对您现在拥有的内容进行许多改进:

http://jsbin.com/epurul/3/edit

访问条目以自己测试代码。以下是内容,为方便起见:

HTML:

<body>
  <button onclick="playGame()">Play Game</button>  
</body>

和 JavaScript:

// Expose playGame as a top-level function so that it can be accessed in the
// onclick handler for the 'Play Game' button in your HTML.
window.playGame = function() {

  // I would generally recommend defining your functions before you use them.
  // (This is just a matter of taste, though.)
  function UCFL(string) {
    return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
  }

  // Rather than capitalize name everywhere it is used, just do it once
  // and then use the result everywhere else.
  function getName(message) {
    return UCFL(prompt(message));
  }

  var name = getName('So what is your name?');

  // Don't repeat yourself:
  // If you're writing the same code in multiple places, try consolidating it
  // into one place.
  var nameAttempts = 0;
  while (!confirm('So your name is ' + name + '?') && ++nameAttempts < 3) {
    // Don't use 'var' again as your name variable is already declared.
    name = getName('Then what is your name?');
  }

  if (nameAttempts < 3) {
    alert('Good. Lets start the roleplay, Sir ' + name + '.');
  } else {
    alert("Oh, guess what? I do not even fucking care what your name is anymore. Let's just start...");
  }

};
于 2013-05-07T23:59:09.123 回答