JavascriptとRails 3.1で変数を宣言するときにエラーが発生します。 私は私のindex.html.erbに以下のコードを置けばまったく問題ありません:私はjavascriptの\ clock.jsファイル\資産にコードを入れ、その後に<%= javascript_include_tag 'clock' %>
を置けばRails 3でJavascript変数を宣言する際のエラー
<div id='show_time_id' style="background-color: #eee">
<script type="text/javascript">
function updateClock ()
{
var currentTime = new Date ();
var currentHours = currentTime.getHours ();
var currentMinutes = currentTime.getMinutes ();
var currentSeconds = currentTime.getSeconds ();
.......
// Update the time display
document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
</script>
<script type="text/javascript">
$(document).ready(
function(){
setInterval('updateClock()', 1000)
});
</script>
</div>
<div style="width: 10em; text-align: right; margin: 20px auto;">
<span id="clock"> </span>
</div>
が、しかし、私のindex.html.erbファイル、私はエラーコンソールでこのエラーを取得する:
構文エラー
http://localhost:3000/assets/clock.js?body=1
するvar CURRENTTIME =新しいDate();
私はindex.html.erbにコードを残すことができるので、それは重要なことではありませんが、私は知りたいと思います。これはどうして起こるのでしょうか?
私はあなたがまだ置くとき、あなたがやっている*正確に*何を示す必要があると思いますそれは 'clock.js'にあります。 – Alexander
これは問題ではありませんが、 'document.getElementById(" clock ")。firstChild.nodeValue'を使って、jqueryコードをすべて読み込むと、奇数になります。 – fantactuka
@Alexander:これはまさに私がやっていることです。 clock.jsを開き、index.html.erbでそれをincludeタグに置き換えます。 –