0

私はjqueryテンプレートを使用しており、HTMLマークアップの下にあります

<div id="results">           
  <div class="CommentItem" commentid="33064" id="33064" data-guid="/Profile/Profile.aspx?id=Charliedog33"> 
    <div class="CommentPic">                 
      <img src="AspNetForumAvatarguy18.jpg">
    </div>             
    <div class="CommentText">thsi is text</div>
  </div>
  <div class="CommentItem" commentid="33064" id="33064" data-guid="/Profile/Profile.aspx?id=Charliedog33"> 
    <div class="CommentPic">                 
      <img src="AspNetForumAvatarguy18.jpg">
    </div>             
    <div class="CommentText">thsi is text</div>
  </div>
</div>

リスト内のデータをレンダリングしますが、個々のアイテムにイベントを登録するにはどうすればよいですか。(ここで、commentid は一意の値を持ちます)

  $("#results").live("click", function (evt) {

        if ($(this).evt.attr('commentid') != null) {
            alert("event registered goes here");
        }
        else {
            alert("there is prolem");
        }

    });
4

3 に答える 3

1

まず、on() を使用する必要があります。これは、live が推奨されていないためです。次のようなことを試してください:

$("body").on("click", ".CommentItem", function () {
    if ($(this).attr('commentid')) {
        alert("event registered goes here");
    }
    else {
        alert("there is prolem");
    }

});
于 2013-03-22T13:16:08.840 に答える
0
 if ($(this).attr('id') != null) {
            alert("event registered goes here");
        }
        else {
            alert("there is prolem");
        }

id が利用できない場合、 $(this).attr("id") は undefined を返します。

于 2013-03-22T13:20:44.930 に答える