0

How to catch $(".tag") click which added by ajax, I try to use .live(), .on() and .bind() but where is no result

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  var content = $("#ad_content").val();
  $.ajax({
    type: "GET",
    url: "tags/check",
    data: {
      content: content
    },
    dataType: "text",
    success: function(tags) {
      var tag, tags_html, _i, _len, _ref;
      if (tags === "[null]") {
        return $(".tags").html("");
      } else {
        tags_html = "";
        _ref = jQuery.parseJSON(tags);
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          tag = _ref[_i];
          tags_html += "<span class=\"tag\">" + tag + "<a class=\"websymbol\" href=\"javascript:;\">Î</a></span>";
        }
        $(".tags").html(tags_html);
      }
    }
  });
  $(".tag").on("click", function() {
    alert("123");
  });
});
</script>

<textarea id="ad_content">2222</textarea>
<div class="tags"></div>
4

2 回答 2

3

尝试这个:

$(document).on('click','.tag', function(){
   alert("123");
});
于 2013-08-31T17:47:33.723 回答
1

使用 .on 函数,它将绑定到 .tag 元素。

它应该看起来像

$("parentelement").on('click','.tag', function(){
   //Do something
}
于 2013-08-31T17:55:21.273 回答