用于按钮的单击事件的选择器
What selector to use for the click event of a button
在我的jQuery中,我添加了一系列li元素,每个li元素都有一个子元素按钮。我想做的是制作一个点击事件处理程序来附加到按钮上,当点击时,它将删除按钮及其父元素li。这是我的HTML:
<div id="empty_message" class="open">
<h3>You have no tasks left to accomplish!</h3>
</div>
<div id="tasklist">
<ol class="list" id="list">
</ol>
</div>
这是我写的jQuery:
$("#add_task").click(function() {
//create a li, save input in there, and add to ol
var new_task = $('<li></li>');
new_task.text($("#add_todo").val());
//new_task.attr('id', 'new_task');
new_task.appendTo('ol.list');
if (new_task)
//create button and attach it to new li element
var new_button = $('<input />').addClass('done');
new_button.attr({
type : "button",
value : "Task Accomplished!"
});
new_button.appendTo(new_task);
}
$("#add_todo").val("").focus();
});
//end click event of Add task button
$('.done[value="Task Accomplished!"]').click(function() {
$(function () {
alert("This is working.");
});
//hide <li> element
$(this).parentElement.remove();
//if list is empty, show empty message
if ($("#list li").length == 0)
$("#empty_message").show();
$("#add_todo").focus();
});
//end click event of Task Accomplished button
添加任务按钮(第一个功能)正在工作。我不明白为什么"任务完成"按钮不起作用!!有人能帮我吗?
$("#add_task").on('click', function() {
//create a li, save input in there, and add to ol
var new_task = $('<li />', {
text : $("#add_todo").val()
});
var new_button = $('<input />', {
'class' : 'done',
type : 'button',
value : 'Task Accomplished!',
on : {
click : function() {
$(this).closest('li').remove();
if ($("#list li").length == 0) {
$("#empty_message").show();
}
$("#add_todo").focus();
}
}
});
$('#list').append(new_task.append(new_button));
$("#add_todo").val("").focus();
});
在创建元素时创建事件处理程序,并使用closest
而不是parentElement
。
相关文章:
- 事件的ExtJS控制器选择器
- 平均值.on(事件[,选择器][,数据],处理程序)
- 日期选择器更改事件抛出”;TypeError:字符串不是函数;
- 使用 jQuery 日期选择器和 Angular 2 更改事件
- 事件冒泡与查询选择器
- 委派的事件处理程序选择器
- 定位将禁用悬停选择器和鼠标悬停事件
- 类似jQuery的主干事件选择器
- EditableField的JQuery Catch-onChange事件-日期选择器
- Kendo日期选择器值方法没有't引发自己的变革事件
- 谷歌Chrome日期选择器事件
- 通过使用不同的选择器选择元素,为元素提供多个事件处理程序
- 引导选择选择器焦点事件
- jQuery类选择器事件绑定是否将eventHandler附加到每个实例
- Ajax数据与日期选择器事件不同步
- JQuery动态选择器事件处理程序
- 这是我的代码,我想添加日期选择器事件,它根据日期给出特定的记录
- 带有通配符选择器/事件冒泡的嵌套元素
- 使用聚合物铁选择器事件
- 如何在不必触发选择器事件的情况下调用jquery函数