试图获得按钮来激活将打印文本的功能
Trying to get button to activate a function that will print text
我正在制作一款基于文本的冒险游戏。现在我只有性别选择。在选择性别之后,您将看到一个屏幕,其中包含一个字符串和一个按钮。我要做的是,一旦按钮被点击,就开始游戏(简单地打印第一个场景的字符串)。
这是我到目前为止的代码…
<!DOCTYPE html>
<head><title>Adventure Game</title>
<link src="/home/logan/game2" type="text/javascript">
</head>
<body>
<script type="text/javascript"></script>
<script>
confirm("Ready to play?");
var age = prompt("How old are you?");
if (age >= 18) {
document.write(" ");
} else {
document.write("You're less than 18? You better just stay home, filthy pansy.");
}
document.write("Please select your gender.")
</script>
<p>
<select id="genderlist">
<option value="null"> </option>
<option value='Male'> Male </option>
<option value='Female'> Female </option>
<option value='Other'> Other </option>
</select>
</p>
<script>
document.getElementById("genderlist").onchange = function() {
var gender=document.getElementById("genderlist").value;
if(gender=="Male"){
document.write("Okay");
document.write('<p><button id="start" onclick="function()"> Lets begin, shall we?</button></p>');
}
else if(gender=="Female"){
document.write("Sounds Good");
document.write('<p><button id="start"> Lets begin, shall we?</button></p>');
}
else{
document.write("I don't think this will be for you.");
}
return false
};
</script>
<script>
document.getElementById("start").onclick = function(){
document.write("<p> This is nice </p>");
};
</script>
</body>
</html>
是否有一些明显的错误我忽略了?
你的问题
问题是您将按钮click
侦听器设置得太早。我的意思是你的<button>
不存在,也没有插入到DOM中。
所以document.getElementById("start")
初始化为null
[more cleaner]我用
.createElement
和。appendchild代替document.write
.[fun]我把确认放在
if
语句中。如果访客没有准备好,他不会玩;)[你的问题]我设置按钮
click
监听器后,按钮已创建并追加在主体
if (confirm("Ready to play?")) {
var age = prompt("How old are you?");
if (age < 18) {
document.body.appendChild( createElement("p", "You're less than 18? You better just stay home, filthy pansy.") );
}
document.body.appendChild( createElement("p", "Please select your gender."));
document.getElementById("genderlist").onchange = function() {
var gender=document.getElementById("genderlist").value;
if(gender==="Male"){
document.body.appendChild( createElement("p", "Okay") );
var button = createElement("button", "Lets begin, shall we?", "start");
document.body.appendChild( button );
// Set listener
document.getElementById("start").onclick = function(){
document.body.appendChild( createElement("p", "This is nice") );
};
}
else if(gender==="Female"){
document.body.appendChild( createElement("p", "Sounds Good") );
var button = createElement("button", "Lets begin, shall we?", "start");
document.body.appendChild( button );
// Set listener
document.getElementById("start").onclick = function(){
document.body.appendChild( createElement("p", "This is nice") );
};
}
else{
document.body.appendChild( createElement("p", "I don't think this will be for you.") );
}
return false
};
}
function createElement(type, text, id){
var el = document.createElement(type);
if (id) {
el.id = id;
}
el.innerHTML = text;
return el;
}
<p>
<select id="genderlist">
<option value="null"> </option>
<option value='Male'> Male </option>
<option value='Female'> Female </option>
<option value='Other'> Other </option>
</select>
</p>
createElement()
函数在这里只是因为我不喜欢重复的代码,我很懒。
相关文章:
- 文本框不是从Javascript/Asp.net中的对话框中打印出来的
- 正在打印图表上的文本
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- 如何在html中打印选择文本
- 如何按骨干.js打印“此”列表项的文本
- 如何使用xpath和Javascript执行器打印文本
- 如何在php中使用echo来在html中打印文本
- 无法在所有页面中打印类似水印的背景文本
- 将所有单选按钮的值打印到文本区域
- 使用javascript/jquery打印文本文件的内容
- 单击时更改路径颜色时打印文本
- Javascript在列中打印文本
- javascript中的打印函数不是打印文本框值
- 试图获得按钮来激活将打印文本的功能
- 通过AJAX调用Perl Script来打印文本文件的内容
- VB.net如何直接打印文本客户端
- 如何在php中打印文本框值
- 如果当前值不是数组中的值,则打印文本
- 如何从UIWebView打印文本到JavaScript表单
- 在Canvas中使用setTimeout从数组中打印文本