我正在用jQuery制作简单的待办事项列表,并且遇到了关于如何通过双击编辑li
元素的内容的问题。我写了一个函数,但它不起作用。li元素的jQuery编辑内容
这里是我的代码:http://jsfiddle.net/strangeviking/1vwho2ru/3/:
function addListItem() {
var text = $('#new-text').val();
$("#todolist").append('<li><input type="checkbox" class="edit" />' + text + ' <button class="delete">Delete</button></li>');
$("#new-text").val('');
};
function deleteItem() {
$(this).parent().remove();
}
function finishItem() {
if ($(this).parent().css('textDecoration') == 'line-through') {
$(this).parent().css('textDecoration', 'none')
} else {
$(this).parent().css('textDecoration', 'line-through');
}
}
function editItem(e) {
var $input = $(e.target).closest('li').addClass('editing').find('.edit');
$input.val($input.val()).focus();
}
$(function() {
$('#new-text').keyup(function(e) {
if (e.keyCode === 13) {
addListItem();
}
});
$(document).on('click', '.delete', deleteItem);
$(document).on('click', '.edit', finishItem);
$(document).on('dblclick', '.edit', editItem);
$("#select_all").click(function() {
$('input:checkbox').not(this).prop('checked', this.checked);
if ($('li').css('textDecoration') == 'line-through') {
$('li').css('textDecoration', 'none')
} else {
$('li').css('textDecoration', 'line-through');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>List</h1>
<input type="text" id="new-text" />
<ul id="todolist"></ul>
<br>
<input type="checkbox" id="select_all" />Select all
@http://stackoverflow.com/users/3541881/%e1%94%95%e1%96%ba%e1%98%8e%e1%95%8a我怎样才能使它的宽度限制只为文字长度?我不想有可能删除复选框和按钮 –
@VasylPylypiv我不明白你。如果您像示例中那样添加了“span”,则无法删除复选框/按钮 - 只能编辑文本。 –
@http://stackoverflow.com/users/3541881/%e1%94%95%e1%96%ba%e1%98%8e%e1%95%8a对不起,我把这段代码写错了地点。现在所有工作都很完美你能否帮助我完成与此代码相关的其他小任务?我需要写fucntion将删除所有选中的元素 –