如何输出变量's在JQuery中的值

How to output a variable's value in JQuery?

本文关键字:JQuery 何输出 输出 变量      更新时间:2023-09-26

嗨,我是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>'