我一直在嘗試使JSBin的所有Javascript頁面代碼在單擊按鈕時自動工作。問題包括無法運行代碼,因爲它說我的腳本中有多個變量不能一起工作,並且無法將它全部放入HTML中,因爲console.log不起作用。我嘗試了一些不同的想法,但不幸的是,我無法正確地做到這一點。如何從HTML中的按鈕運行整個Javascript頁面?
我的代碼是:
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>
我不知道實際的問題是什麼。你想達到什麼目的?預期的結果是什麼?實際結果與它有什麼不同?您的示例代碼是否可以簡化以僅顯示問題的本質? – millimoose 2013-05-07 23:31:03
我試圖讓他們連接點擊按鈕,使整個Javascript頁面運行... – user2103896 2013-05-07 23:33:55
@ user2103896那麼把它放在一個函數中,然後在onclick中提到函數(如果你真的必須在內聯那樣) - http://jsfiddle.net/tAMfj/ – lifetimes 2013-05-07 23:34:31