如何输出变量's在JQuery中的值
How to output a variable's value in JQuery?
嗨,我是JQuery的新手,我正在为一个大学一年级的项目制作一个基于文本/命令行的游戏等。
我正在尝试创建一个库存功能,我有一个变量,其中包含玩家可以持有的一个物品。然后,如果用户键入"inhand",我希望输出为"you are holding"inhand。到目前为止,这是我的部分代码,但我无法让变量输出——它只是空白的。
var inhand = "nothing";
//torch
else if (input == "pickup torch") {
$('<p>You picked up the torch.</p>').insertBefore("#placeholder").fadeIn(1000);
inhand = "torch";
}
//inhand
else if (input == "inhand") {
$('<p>You are currently holding a </p>' + inhand ).insertBefore("#placeholder").fadeIn(1000);
}
正如你在倒数第二行看到的,我有"你在抱"+inhand。这不起作用,但它只是留下了一个空白。感谢
<!DOCTYPE html>
<html>
<head>
<script src="scripts/jquery.js" type="text/javascript"></script>
<link href='https://fonts.googleapis.com/css?family=Comfortaa|Poiret+One' rel='stylesheet' type='text/css'> <!--Google fonts-->
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'> <!--Google fonts-->
<link rel=stylesheet href="scripts/AlbaCSS.css" type="text/css" />
<title>Alba</title>
</head>
<body>
<div id="container">
<div id="console">
<p id="message_begin">Welcome to Alba. At any time you may type "help". Also try using 'pickup [itemname]' to collect items.</p>
<p id="area_main">Alba finds herself at the opening of a forest. Visability is low but she can see paths: 'north', 'east', 'west' and also a 'gate'. On the floor there is a key slightly hidden under a pile of leaves.</p>
<p id="message_help" style="display:none;">Help: <br />
<i>A</i><br />
<i>list</i><br />
<i>of</i><br />
<i>commands</i><br />
<i>would</i><br />
<i>appear</i> </p>
</div>
<!-- ALLOWS CONTENT TO BE INSERTED BEFORE THIS -->
<div id="placeholder"></div>
</div>
<!-- INPUT -->
<form onsubmit="return false;">
<center><input type="text" id="command_line" size="50" autofocus="autofocus" /></center>
</form>
</div>
<script type="text/javascript" src="scripts/game.js"></script>
</body>
</html>
// JavaScript Document
// Variables for the game
var inhand = "nothing"; //For picking up items
var lifepoints = 1; //How many lives Alba has
$(document).ready(function() {
$("#message_begin").fadeIn(3000);
$("#area_main").fadeIn(3000);
$("#command_line").fadeIn(3000);
$("form").submit(function() {
var input = $("#command_line").val();
//help
if (input.indexOf("help") > -1) {
if (input == "help") {
$("#message_help").clone().insertBefore("#placeholder").fadeIn(1000);
}
}
//end help
//pickup
else if (input == "pickup") {
$('<p>pickup what? Be specific. Type "help" for a list of all commands.</p>').insertBefore("#placeholder").fadeIn(1000);
}
//key
else if (input == "pickup key") {
$('<p>You picked up the key.</p>').insertBefore("#placeholder").fadeIn(1000);
inhand = "key";
}
//torch
else if (input == "pickup torch") {
$('<p>You picked up the torch.</p>').insertBefore("#placeholder").fadeIn(1000);
inhand = "torch";
}
//inhand
else if (input == "inhand") {
$('<p>You are currently holding a </p>' + inhand ).insertBefore("#placeholder").fadeIn(1000);
}
else if (input != "") {
$('<p>I don''t understand "' + input + '"</p>').insertBefore("#placeholder").fadeIn(1000);
}
$("#console").scrollTop($("#console")[0].scrollHeight);
$("#command_line").val("");
});
});
我假设
'<p>You are currently holding a </p>' + inhand
应该是
'<p>You are currently holding a '+inhand+'</p>'
相关文章:
- 在jQuery中输出单击单元格的内容
- jQuery鼠标输出调用CSS3动画
- 在 SQL 查询中使用 Jquery DatePicker 输出
- 为什么jQuery减法输出NaN
- jQuery没有输出
- jQuery - 仅输出偶数
- 通过 PHP 文件查询的表单条目和使用 JQuery AJAX 输出 PHP 文件的结果
- Jquery 删除输出上的头部和身体标签
- Jquery/php 输出自定义错误消息
- jQuery对输出的Ci/Sql数据进行迭代
- Jquery-如何输出“$(this).val()“;在html中
- jQuery Mobile输出问题
- Ajax$.post()函数jquery的输出
- 使用Backbone Collection时,无法通过Jquery分别输出所有JSON项
- JQuery“greying"输出表单中的文本字段
- 最后一个元素类名没有在jQuery中输出
- PHPStorm错误,jQuery未输出表
- 重新计算表单's使用jquery的输出
- 将参数传递给 @Url.Action 基于 jquery hasClass 输出
- 通过JSOn传递PHP数组,以便在Jquery中输出