0

你好,我对做 javascript 和 jquery 有点新,请帮我解决我的问题。我真的很感激。谢谢!

在第 7-8 页上,如何使用 jquery 删除“新游戏”按钮上的“禁用”?

这是 index.html:

 <!DOCTYPE>
<html>
<head>
    <link href="assets/css/blackjack.css" type="text/css" media="screen" rel="stylesheet">
    <script src="assets/js/Modernizr.js"></script>
    <script src="assets/js/jquery.js"></script>
    <script src="assets/js/Mustache.js"></script>
    <script src="assets/js/blackjack.js"></script>

</head>

<body>

    <div class="wrapper">

    <img src="assets/images/rocket-u-logo-large.png">
    <h1>Blackjack</h1>

    <p>Hi, thanks for stopping by our blackjack table. Pull up a chair and let's play...</p>

        <div id="card-table">

            <h2>Dealer</h2>

            <div id="dealer-hand"></div>

            <div id="status"></div>

            <div id="player-hand"></div>

            <h2>Player</h2>

                <div id="player-options">
                    <button class="bj" id="new-game" disabled>New Game</button>
                    <button class="bj" id="hit">Hit</button>
                    <button class="bj" id="stand">Stand</button>

                </div>

        </div>




    </div>


</body>
</html>

这是js:

$('#bj').click(function () {
    $('#hit').show();
    $('#stand').show();
});


function initGame() {

    var initErrors = [];
    var errorMessage;

    // Test if browser supports local storage
    if (Modernizr.localstorage) {
        // console.log("Local storage is supported.");
    } else {
        var errorStatus = "Local storage is not available"
        // console.log(errorStatus);
        initErrors.push(errorStatus);
    }

    // Test if browser supports mustache.js
    var mustacheScript = $('script[src*="js/Mustache.js"]').length; 
    if (mustacheScript != 0) {
        // console.log("Mustache loaded!");
    } else {
        var errorStatus2 = "Mustache not loaded."
        // console.log(errorStatus2);
        initErrors.push(errorStatus2);
    }



        function displayErrorMessage() {
        // Test if initErrors array has any errors
            if (initErrors.length != 0) {
                    if (errorStatus2 === undefined) {
                        errorStatus2 = "";
                        } else if (errorStatus === undefined) {
                                   errorStatus = "";
                    } 
            var errorMessage = "Houston, we have a problem (" + errorStatus + ', ' + errorStatus2 + ").";
            // console.log(errorMessage);
            $('#status').append("<p>" + errorMessage + "</p>");
            } else {
            var successMessage = "Ready to play? Click 'New Game' to start...";
            $('#status').append("<p>" + successMessage + "</p>");
            // console.log(successMessage);
            }

        }

        displayErrorMessage();


    //Test 'boolean' return values
    if (initErrors.length != 0) {
        return false;
        $('#new_game').attr("disabled", "disabled");
        } else {
        return true;
        $('#new_game').removeAttr("disabled");
    }


}   


console.log(initGame());

$(document).ready(function () {

    initGame();

});
4

4 回答 4

2

您自己编写了代码。但它在 return 语句下面,这将使它可以访问。

带上下面的退货声明

$('#new_game').removeAttr("disabled");

它应该工作。

于 2013-11-05T08:32:26.043 回答
0
$('#new-game').removeAttr('disabled');

看起来您的 JS 代码有错误:在 HTML 中<button class="bj" id="new-game",但在 JS 中$('#new_game').removeAttr("disabled");。您在 id 中使用下划线而不是“-”。

于 2013-11-05T08:29:58.887 回答
0

你可以试试这个:

$('#new_game').prop('disabled',false);
于 2013-11-05T08:35:35.703 回答
0

您可以使用下面列出的任何一个

$('#new_game').attr("disabled", false);

或者

$("#new_game").removeAttr("disabled");

或者

$("#new_game").prop("disabled",false);
于 2013-11-05T08:36:13.283 回答