0

我以前从一个可爱的社区成员 (Oshawott) 那里得到了一些关于这个脚本的帮助,但我被困在一个似乎是小错误的问题上,我不知道如何解决与 Jquery 相关的问题。只是为了仔细检查,我认为我已经在我的脚本中正确地实现了 Jquery,但由于我还是新手,我正在努力找出问题所在。它说它是一个“未终止的字符串常量”,它来自该$(document).ready(function refresh(){部分。不幸的是,我没有找到解决这个问题的方法,我已经阅读了各种论坛,但我似乎无法理解它。我希望你们能帮助我,我很感激你给我的任何时间来帮助我变得更好。

JS:

$(document).ready(function refresh() { 
    // This function takes every table cell with dusky class 
    // and inserts output of duSky() into it
    $($dusky).each(function () {
        this.html = duSky();
    });
});

代码

<html>
<head>
<script src = "http://code.jquery.com/jquery-1.9.1.min.js"></script>
<body>
<body bgcolor="33FF00">
<script language = "JavaScript">


function duSky(){                                                   //This is a function to tell add points to the points variable if the user clicks a duck.
    duckNum = Math.floor((Math.random()*10)+1)
    if(duckNum<10){document.write("<img src=images/skyTile.jpg>")}
    else{document.write("<img src='images/duckTile.jpg' onClick='duckClick()'")}
    }


</script>


<td class='dusky'></td>




<center><img src=images/duckHuntTitle.gif><br>                  <!Duck Hunt title gif, no background so you can see the background of the page, also centered>



                                    <!Named the table "TableDiv" so that I can refer to it at a later date. This was to try and make my job of refreshing easier>   
        <table>
        <tr>
        <td>  </td>
        <td>  </td>
        <td>  </td>
        <td>  </td>
        <td>  </td>

        </tr>                                                       <!Inside of all the table boxes there is a function that designates whether inside will be a duck or sky tile>
        <tr>                                                        <!This is the duck table that is exactly 1000px wide by 400px height. This is created by 10 200px by 200px boxes, two rows of 5 boxes>
        <td>  </td>
        <td>  </td>
        <td>  </td>
        <td>  </td>
        <td>  </td>
        </tr>
        </table>


<form name="score">
Points <input type="text" name="pointsscored" readonly="readonly">  <!This is the box that is centered that displays the points the player has got, also is now readonly so no tweaking is allowed>
</form>

<form name="timer">
Time <input type="text" name="timeBox" readonly="readonly">         <!This is the timer box that is centered as well that displays how long the player has left and is readonly>
</form>
</center>


<script language = "JavaScript">                                    //Returns the script to JavaScript to allow for functions to be used that are related to the HTML previous to this




$(document).ready(function refresh(){                               // This function takes every table cell with dusky class and inserts output of duSky() into it
    $($dusky).each(function(){
        this.html = duSky();
    })
})

setInterval(refresh, 1000);                                         // Make our refresh script run every 5 seconds

timeLeft = 30                                                       //this is counted down from until it hits 0
points = 0                                                          //this is the points system that is added to by 10 each time a duck is clicked

document.timer.timeBox.value = timeLeft                             //Displays the time left before the game has even started and been clicked so the player immediately knows the time that they have to play with


function timeDecrease(){                                            //This is the timer function that reduces the timer by a second each time to make the game slowly time out after 30 seconds
setInterval(function(){timeLeft--                                   //I am still working on the refresh function but hopefully it will be corrected to make the table refresh with every 1000 miliseconds
document.timer.timeBox.value=timeLeft;
//document.tableDiv.reload(true)                                    //trying to get the reload function to work.
},1000);                                                            //1000 miliseconds, therefore it is 1 second
}


while(timeLeft < 0){alert("Timeeeeeees Up, you scored: ", points ,"points! well done Duck Slayer!")}    //Alert to signify the end of the game.


// ----------------Function for clicking the duck-----------------


function duckClick(){
points = points + 10;
document.score.pointsscored.value = points;                         //when the player clicks the duck, points will be added to the points box
}




</script>
<center>


<form name = "playButton"> 
<button type="button" onClick = "timeDecrease()">Play!</button>     <!This is the on click function that starts the game/countdown feature>


</center>
</form>     
</body>
</html>
4

5 回答 5

0

取出refresh,因为您使用的是匿名方法(它没有名称)

$(document).ready(function(){
   // rest of code here
});

或者你可以做这样的事情

var refresh = function(){
    // code hrere
}
$(document).ready(refresh);
于 2013-05-09T10:46:36.093 回答
0

尝试定义refresh外部就绪范围:

var refresh;

$(document).ready(function () { 
    refresh = function(){
        $($dusky).each(function () {
            this.html = duSky();
        });
    }
});
于 2013-05-09T10:53:53.733 回答
0

你需要使用

$(document).ready(function(){
});

使用 in ready 函数,您可以编写 javascript 函数。

于 2013-05-09T10:46:57.793 回答
0

一些东西...

下面的代码是不对的:

$(document).ready(function refresh(){                                                           
    $($dusky).each(function(){
        this.html = duSky();
    })
})

将其替换为:

$(document).ready(function(){
  function refresh () {                                                           
    $($dusky).each(function(){
        this.html = duSky();
    });
  }
  // And if you wanna call the function on load, use this:
  refresh();
})

而且你缺少很多分号和一些地方,字符串没有完全闭合。例如:

duckNum = Math.floor((Math.random()*10)+1)
if(duckNum<10){document.write("<img src=images/skyTile.jpg>")}
else{document.write("<img src='images/duckTile.jpg' onClick='duckClick()'")}

将其更改为:

duckNum = Math.floor((Math.random()*10)+1); // Semi colon missing
if(duckNum<10){document.write("<img src=images/skyTile.jpg>");}
else{document.write("<img src='images/duckTile.jpg' onClick='duckClick()'>");} // Tag end missing
于 2013-05-09T10:47:50.453 回答
0

尝试这个:

$(document).ready(function() { 
    // This function takes every table cell with dusky class 
    // and inserts output of duSky() into it
    $('table tr td.dusky').each(function () {
        $(this).html(duSky());
    });
});
于 2013-05-09T10:50:09.017 回答