2013-05-07 63 views
-6

我一直在嘗試使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> 
+2

我不知道實際的問題是什麼。你想達到什麼目的?預期的結果是什麼?實際結果與它有什麼不同?您的示例代碼是否可以簡化以僅顯示問題的本質? – millimoose 2013-05-07 23:31:03

+0

我試圖讓他們連接點擊按鈕,使整個Javascript頁面運行... – user2103896 2013-05-07 23:33:55

+1

@ user2103896那麼把它放在一個函數中,然後在onclick中提到函數(如果你真的必須在內聯那樣) - http://jsfiddle.net/tAMfj/ – lifetimes 2013-05-07 23:34:31

回答

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..."); 
    } 

}; 
+0

你是一個真正的專家.. – user2103896 2013-05-08 00:26:02

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> 
+0

我以前做過,無法console.log .. – user2103896 2013-05-07 23:37:52

相關問題