【问题标题】:each() function not in the DOM, delegeta problems jqueryeach() 函数不在 DOM 中,delegeta 问题 jquery
【发布时间】:2020-09-06 20:15:14
【问题描述】:

我想在添加新行后显示alert()

这是我的代码,但委托不起作用。

$(".tweet").each(function() {
  alert("tweet alert");
});

$("button").click( function(){
    $("#container").append("<div class='tweet'>tweet</div>");
    
    
});


$(document).delegate('click', function(){
    $(this).addClass("tweet");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>

<div id="container"></div>
<button>Create</button>

我使用的是基本的 jquery 版本1.4

在我的代码中,警报根本不显示!做错了什么?谢谢。

【问题讨论】:

  • 为什么使用1.4?当您附加 div 时,为什么不在按钮单击处理程序中添加警报?
  • 1.4,因为我想要一个快速的插件加载,到目前为止我的代码不需要 mor
  • 添加点击处理程序是什么意思?

标签: javascript jquery each


【解决方案1】:

您可以像这样在点击处理程序中添加警报:

$("button").click(function() {
  $("#container").append("<div class='tweet'>tweet</div>");
  alert("tweet alert");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<div id="container"></div>
<button>Create</button>

【讨论】:

  • 不,我希望each() 函数具有alert(),因为这是一个更复杂的代码
  • 这仅适用于已经存在的元素。不是新的
  • 这是我的问题,伙计。我需要让这些新元素发挥作用
猜你喜欢
  • 1970-01-01
  • 2013-09-28
  • 2011-01-07
  • 2011-05-22
  • 2011-11-26
  • 2014-04-05
  • 2012-02-06
  • 2015-07-17
相关资源
最近更新 更多