0

アイテムの配列を受け取る js 関数があります。関数自体は、jquery html 関数の呼び出しを使用して動的に html コンテンツを作成します。問題は、追加された html 内に配列を正しく埋め込む方法を覚えていないことです。例えば、

var ErrorsNotifier = {
   init: function(items) {
        var template = '<div id="message">'; 
        template += '<div id="id">some message.<br /><br />'; 
        template += '<div class="errorsList">error desc</div></div>'; 
        template += '<a href="#" class="goback" onclick="ErrorsNotifier.Back("+items+");"></a>'; 
        template += '<a href="#" class="proceed" onclick="ErrorsNotifier.Next();"></a>'; 
        template += '<input type="image" src="someimage" alt="" class="" onclick="window.open("someurl");"/></div>'; 
        $("#content").html(template);
   }
}

HTML が本文にレンダリングされた後、アンカー タグをクリックすると、次のエラーが返されます -Uncaught SyntaxError: Unexpected identifier

ブラウザでアンカータグを調べると、次のように表示されます

a href="#" class="goback" onclick="ErrorsNotifier.Back([object Object]);"
4

2 に答える 2

2

試す

var ErrorsNotifier = {
   init: function(items) {
       ...
       var $template = $(template);
       $template.find('a.goback').on('click', function(){ErrorsNotifier.Back(items);});
       $("#content").empty().append($template);
   }
}

.on()イベントハンドラーをアタッチするために使用したことに注意してください。

于 2012-11-26T21:32:30.763 に答える
0
var ErrorsNotifier = {
 init: function(items) {
    var template = '<a href="#" class="goback" onclick="ErrorsNotifier.Back(\''+items+'\');">Link</a>';
    $("#content").html(template);
   },
 Back: function(x){ alert(x); }
};

これをチェックして

于 2012-11-26T21:40:40.290 に答える