gpt4 book ai didi

javascript - 带有编辑按钮 Jquery 的待办事项列表

转载 作者:行者123 更新时间:2023-12-01 04:03:40 24 4
gpt4 key购买 nike

我正在尝试制作一个带有编辑按钮的待办事项列表,单击该按钮后,将使添加的项目可编辑,但遇到了麻烦。我已经创建了按钮和所有内容,但是当我单击它时什么也没有发生。任何建议将不胜感激!

JavaScript

function editItem(){
var parent = $(this).parent();
if (!parent.hasClass('edit')) {
parent.addClass('edit');
}else if (parent.hasClass('edit')) {
var editTask = $(this).prev('input[type="text"]').val();
var editLabel = parent.find('label');
editLabel.html(editTask);
parent.removeClass('edit');
}

$(function(){
$(document).on('click', 'edit', editItem)
});

最佳答案

您的目标似乎是 <edit> ,你应该使用 .edit :

$(function(){
$(document).on('click', '.edit', editItem);
});

工作片段

$(function () {
function addItem () {
// append to the list
$("#todo-items").append('<li><span>' + $("#todo").val() + '</span> <small><a href="#edit">Edit</a> &bull; <a href="#delete">Delete</a></small></li>');
// clear the text
$("#todo").val("");
}
$("#todo").keydown(function (e) {
// if enter key pressed
if (e.which == 13)
addItem();
});
// on clicking the add button
$("#add").click(addItem);
// delegate the events to dynamically generated elements
// for the edit button
$(document).on("click", 'a[href="#edit"]', function () {
// make the span editable and focus it
$(this).closest("li").find("span").prop("contenteditable", true).focus();
return false;
});
// for the delete button
$(document).on("click", 'a[href="#delete"]', function () {
// remove the list item
$(this).closest("li").fadeOut(function () {
$(this).remove();
});
return false;
});
});
* {font-family: 'Segoe UI'; margin: 0; padding: 0; list-style: none; text-decoration: none;}
input, li {padding: 3px;}
#todo-items small {display: inline-block; margin-left: 10px; padding: 2px; vertical-align: bottom;}
#todo-items span:focus {background-color: #ccf;}
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<input type="text" id="todo" />
<input type="button" value="Add" id="add" />
<ul id="todo-items"></ul>

关于javascript - 带有编辑按钮 Jquery 的待办事项列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34560202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com