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

标签 javascript jquery edit

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

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/

相关文章:

javascript - 使用 jquery 添加选项来选择克隆表行中的元素会导致克隆元素外部的选择选项被修改

javascript - 是否可以捕获 CTRL+W 快捷方式并防止选项卡关闭?

jQuery Slider 不适用于保存的图像

android隐式 Intent 编辑无法加载图像

javascript - 带有 TypeScript 的 JQWidget

javascript - javascript中的动态对象构造?

javascript - Material Design Lite 动态改变颜色

Jquery 选择关注页面加载(onLoad?)

java - 在 Java 中现有数据之间添加文本文件中的数据

css - PrestaShop (1.6) 移动登录按钮