如何从HTML中的一个按钮运行整个Javascript页面?
How Do You Run the ENTIRE Javascript Page From An Button In HTML?
我一直在尝试使我所有的Javascript页面代码从JSBin自动工作时点击一个按钮。问题包括无法运行代码,因为它说我的脚本中有多个变量不能一起工作,并且无法将其全部放入HTML中,因为console.log不起作用。我尝试了几个不同的想法,但遗憾的是,我不能做正确的。
My Code Is:
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>
我在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...");
}
};
将代码放入函数中,例如:
<!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>
相关文章:
- 将文本框链接到由按钮运行的javascript公式
- 我的HTML按钮没有在Javascript中运行
- 单击按钮前运行事件
- 从页面javascript按钮运行Node.JS
- 如何通过获取用户输入和使用按钮来运行JavaScript函数
- 在Node Webkit应用程序中从DOM单击按钮时运行批处理文件
- 计时器自动运行,而不是单击javascript按钮
- 如何使用按钮'的OnClientClick属性,以便在ASP.NET中运行一些多个JavaScript函数
- 单击1个表单中的2个提交按钮中的1个时,如何运行脚本
- 在按钮'上运行PHP代码;s onclick事件,或者使用Ajax
- 在同一页面上单击按钮运行不同的代码
- 我如何使用按钮而不是悬停运行此 javascript 代码
- 按钮未运行为按钮,找不到如何使运行JS文件
- 谷歌扩展:如何在弹出窗口中按下按钮时在我的javascript文件中运行函数
- 附加 jQuery 代码以在加载和按钮单击时运行
- 如何制作按钮.单击“开始时不运行”
- 在javascript中,无法在运行时获取按钮的onClick事件的函数
- 每次用户单击按钮时运行if语句检查器
- 每次加载页面时都运行按钮回调函数
- 在不单击按钮的情况下运行按钮中的代码