简体   繁体   English

使用 jQuery 创建列表项后如何修改它?

[英]How can I modify a list item after I created it with jQuery?

This is my todo list what I made.这是我做的待办事项清单。 (To make my code more readable I tried to comment. I hope it worked.) (为了使我的代码更具可读性,我尝试发表评论。我希望它有效。)

If you try my demo, you can add li items with the "add" button.如果您尝试我的演示,您可以使用“添加”按钮添加 li 项目。 And the li items has 2 more buttons. li 项目还有 2 个按钮。 I want to create a modify button to users with can modifying their list items.我想为可以修改列表项的用户创建一个修改按钮。 I have no idea what I can to do for this with jQuery.我不知道我能用 jQuery 做什么。

Second problem is that list buttons don't work perfectly well.第二个问题是列表按钮不能很好地工作。 Because if I use once a delete button it change the done button to delete an item.因为如果我使用一次删除按钮,它会更改完成按钮以删除项目。 But if I dont use the delete button, the done button is working well.但是,如果我不使用删除按钮,则完成按钮运行良好。 I dont know why, maybe the same class name is the problem?我不知道为什么,也许相同的 class 名称是问题所在?

Here is my demo in JS Bin: https://jsbin.com/natiziqawa/edit?html,js,output这是我在 JS Bin 中的演示: https://jsbin.com/natiziqawa/edit?html,js,output

   $(document).ready(function(){
     $("#add").click(function(){

 // Created list elements and their's buttons
    var list_item =document.createElement("li");
      var remove_button=document.createElement("button");
      var done_button=document.createElement("button");
     


 // append the buttons some string to caption buttons
   

   $(remove_button).append("Delete");
   $(done_button).append("Done");


   


 
 // added class name the items to to distinguish them 

  $(list_item).addClass("item")
  $(remove_button).addClass("delete"); 
  $(done_button).addClass("done"); 
  



 // filled  the created items with the input values
 
 var list_text =$("#input").val();
 var time=$("#time").val();
 
 
 // filled the list item with their's  buttons and class names and input values

 $(list_item).append(time,"   :   ",list_text,done_button,remove_button); 

 // finally fill the ul with list items but first check out what is written in the input


 if(input.value==""){
   alert("Please enter an activity")
 
  }


  // If the input has some value  can go 

  else{
 $("ul").append(list_item);

 // after clicked, clear the input field
 $("#input").val("");
 
  


// list item's buttons


$(".done").click(function(){

  $(list_item).click(function(){
    $(this).css("color","white")
    $(this).css("text-decoration","line-through")
  }); 
});





$(".delete").click(function(){
    $(list_item).click(function(){
      $(this).remove()
    }); 
  });

}

  });// main function close
  
}); // document ready close

The idea behind my solution is to use the contenteditable attribute for the added fields.我的解决方案背后的想法是对添加的字段使用contenteditable属性。 To do this, you need to dynamically wrap the first two fields in an additional div, as shown here:为此,您需要将前两个字段动态包装在一个额外的 div 中,如下所示:

$(list_item).append('<div class="edit_item" contenteditable="true">'+time+'</div>',"   :   ",'<div class="edit_item" contenteditable="true">'+list_text+'</div>',done_button,remove_button); 

You will need to replace this code with your existing one.您需要将此代码替换为现有代码。 For a better understanding, this code is located in the comment: "// filled the list item with their's buttons and class names and input values"为了更好地理解,此代码位于注释中: “// 用他们的按钮和 class 名称和输入值填充列表项”

Also, you need to add this rule to the css to align the fields:此外,您需要将此规则添加到 css 以对齐字段:

.edit_item {
  display: inline-block;
}

And regarding the second question:关于第二个问题:

You had a targeting problem.您遇到了定位问题。 You must refer to the current list_item using the closest() method.您必须使用closest()方法引用当前的list_item

For mark:对于标记:

$(".done").click(function(){
   $(this).closest(list_item).css("color","white")
   $(this).closest(list_item).css("text-decoration","line-through");
});

For removing:对于删除:

$(".delete").click(function(){
   $(this).closest(list_item).remove();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将最喜欢的项目添加到新创建的列表中 - How can i add a favorite item to a newly created list 如何使用jquery突出显示所选列表项? - How can I highlight a selected list item with jquery? 填充后如何从选择列表中选择项目? - How can I select an item from a select list after populating it? 在连接到可拖动列表的jQuery可排序列表中,如何告诉可拖动项目放置到的特定列表项? - in a jQuery sortable connected to a draggable list, how can I tell the particular list item that the draggable item was dropped onto? 我确认后如何修改代码以删除项目? - How to modify the code to delete item after I confirm? Sharepoint Jquery-关闭列表项上的“编辑属性”窗口后,我可以使脚本运行吗? - Sharepoint Jquery - can i make a script run after you close the 'Edit Properties' window on a list item? 在此项目中按下 TouchableOpacity 后,如何将焦点设置为列表项中的一个 TextInput? - How can i set focus to only one TextInput in list item after TouchableOpacity pressed in this item? 如何将用户创建的列表项保存在本地存储中? - How do I save a list item created by user in local storage? 如何使 JavaScript 创建的列表项可点击? - How do I make a JavaScript created list item clickable? 当我将鼠标悬停在下拉列表中的某个项目上时,如何显示JQuery工具提示? - How can I get the JQuery Tooltip to display when I hover over an item on a dropdown list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM