点击事件不工作的新HTML与追加,即使使用on
click event not working on new html with append , even using on
嗨,我正在创建一个新的元素和beforesend
在Ajax元素的子元素。
之后我想显示子元素如果我点击它的父元素…
$('#submit-task').on('click', function() {
// variable declarations etc
jQuery.ajax({
// other data
beforeSend: function() {
jQuery('#' + month + ' .month-tasks-design').append(
'<div class="month-task" data-id="' + next_id + '" style="width:0%;background: ' + colour + ';"><div class="task-info">Name: ' + name + '<br />Design Hours: ' + design_hours + '<br />Description: ' + description + '<br /></div></div>');
jQuery('#' + month + ' .month-tasks-dev').append(
'<div class="month-task" data-id="' + next_id + '" style="width:0%;background: ' + colour + ';"><div class="task-info">Name: ' + name + '<br />Development Hours: ' + dev_hours + '<br />Description: ' + description + '<br /></div></div>');
jQuery('.month-tasks-design div[data-id=' + next_id + ']').animate({ width: design_width + '%'}, 1000, function() {});
jQuery('.month-tasks-dev div[data-id=' + next_id + ']').animate({ width: dev_width + '%'}, 1000, function() {});
},
// other data
});
});
$('.month-task').on('click', function(e) {
if ( e.target !== this ) { return; }
var task_info = $(this).find('.task-info');
$(task_info).fadeIn(200);
});
然而,当我点击.month-task
时什么也没发生,它确实工作,但与页面加载的html,只是不与新生成的html
尝试在此上下文中使用event-delegation
,
$(document).on('click','.month-task', function(e) {
if ( e.target !== this ) { return; }
var task_info = $(this).find('.task-info');
$(task_info).fadeIn(200);
});
注意:在document的位置,您应该使用附加元素的静态父元素
这不是委派的方式,比如下面
$(document).on('click', '.month-task', function(e) {
相关文章:
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 表追加而不附加最后一个元素
- I'我设计了一个下拉菜单,onclick会出现,而on blur会消失
- window.on.scroll事件未启动
- 如何在jquery中停止在更改时追加值
- Socket.io on Mozilla Rhino
- CSS中的游标属性似乎不适用于USB On The Go
- jQuery点击ON现在使用.load触发关闭
- JQuery.on(“keydown”)追加到页面时不工作
- 使用.on动态添加jquery/js不知道的html元素
- angularjs$valid-on-dates在firefox中报告错误
- addEventListener on NodeList
- 当我在节点上拖动鼠标时,我如何防止使用d3.ehavior.zoom().on(“缩放”,重绘)
- jQuery 1.7.2:.on()在一个页面上工作;Don’别再干别的了
- Jquery on单击“显示信息”
- 平均值.on(事件[,选择器][,数据],处理程序)
- jQuery[button.class]未检测到用按钮追加行
- 多个jQuery.on()事件无法运行xBrowser(取决于顺序)
- 如何使jQuery的“bind”或“on”事件处理程序幂等
- 点击事件不工作的新HTML与追加,即使使用on