0
var dayNumberCell = doc.createElement('td');
  dayNumberCell.className = 'days_link';
      dayNumberCell.setAttribute('onclick', function(scope) {
                                            var bindScope = function()    {
                                                scope.changeDate(this, this.id);
                                                scope.returnDate(scope.month, scope.year);
                                            };
                                            return bindScope;
                              }(this));

上記のコードは、tdsonclickイベントリスナーを生成します。生成されたコードは次のようになります

    <td class="days_link" onclick="function () {
    scope.changeDate(this, this.id);
    scope.returnDate(scope.month, scope.year);
}">15</td>

TDをクリックすると、構文エラーメッセージが表示されます。指定されたコードが実行され、構文エラーがない関数をどのように記述すればよいですか。

前もって感謝します。

4

4 に答える 4

0

関数定義を文字列リテラル内に配置します。それはコードを期待していて、それが持っているのはテキストだけです。

于 2010-01-28T14:03:23.923 に答える
0
    <td class="days_link" onclick="function () {
    scope.changeDate(this, this.id);
    scope.returnDate(scope.month, scope.year);}">15</td>

関数の外で試しましたか?

    <td class="days_link" onclick="scope.changeDate(this, this.id);scope.returnDate(scope.month, scope.year);">15</td>

それが機能しない場合は、JavaScript ファイルで試してみてください。はい、関数を使用してください。

于 2010-01-28T15:24:54.997 に答える
0

You should rewrite your onclick handler as follows:

dayNumberCell.onclick = function(e) {
   var target = e.target || e.srcElement;
   target.changeDate(target, target.id);
   target.returnDate(target.month, target.year);

   return false;
};

I don't understand why you would try to set the inline click handler from an external js file. You took the right step to remove inline click handlers from your html, but when you set the click handler from an external script, you should not be setting the onclick attribute.

In addition to the way I highlighted, you could use the w3c and microsoft event handlers so that you can attach multiple onclick events to the same element. However, this is a more complicated approach since different browsers handle it differently. This will suffice as long as you don't plan to have other onclick handlers attached to the same cell.

于 2010-01-28T15:04:16.733 に答える
0

おそらく、JavaScript ライブラリを使用したくないと思われるかもしれませんが、これには YUI Event ユーティリティを使用することを強くお勧めします。ダスティン・ディアスは、これをどのように簡素化するかを説明する素晴らしい記事を書いています。

http://www.dustindiaz.com/yahoo-event-utility/

于 2010-01-28T21:06:08.737 に答える