简体   繁体   English

如何使用jQuery制作可编辑的UL LI列表?

[英]How do I make an editable UL LI list using jQuery?

I need your help with something. 我需要你的帮助。 I am attempting to try and add the ability for the user to be able to real time edit a UL LI list by being able to click on the LI element. 我试图尝试增加用户能够通过单击LI元素来实时编辑UL LI列表的功能。 Then, upon clicking on the LI element, the element is changed to that of input box with the LI's current text value. 然后,在单击LI元素时,该元素将更改为具有LI当前文本值的输入框。

Then, when the user clicks off of the LI element the input box goes away and the LI element is returned to its normal state with the newly updated value. 然后,当用户单击LI元素时,输入框将消失,LI元素将以新更新的值返回其正常状态。

I am trying to accomplish the above, however, with the current state of my code, it just adds another box, and does not actually allow me to click inside of it to add a new value. 我正在尝试完成上述操作,但是,使用我的代码的当前状态,它只是添加了另一个框,并且实际上不允许我单击其内部来添加新值。 And also, when I click onto a new LI element, it just keeps adding another box and so on and so on. 而且,当我单击一个新的LI元素时,它只会继续添加另一个框,依此类推。

Here's a fiddle: https://jsfiddle.net/0o0n8rea/ 这是一个小提琴: https : //jsfiddle.net/0o0n8rea/

Here's the code in question: 这是有问题的代码:

window.onload = function() {

    $("#refdocs_list li").click(function(){

        $(this).html('<input type="text" value='+ $(this).text() +'>').focus()

    });

    $("#refdocs_list li").focusout(function(){

    });

}

Here is the HTML markup: 这是HTML标记:

<div class="field_outline" style="background: #FFF; min-height: 75px; max-height: 300px; overflow-y: auto;">

    <ul id="refdocs_list" style="list-style-type: none; margin: 0; padding: 3px 0px 3px 3px;">

        <li>test1</li>
        <li>test2</li>
        <li>test3</li>

    </ul>

</div>

Keep things simple, add the contenteditable property to the element. 保持简单,将contenteditable属性添加到元素。

JSFiddle 的jsfiddle

Fiddle 小提琴

You could wrap a span around your text and then replace that element with the input and vice versa using jQuery's replaceWidth() and a common attribute as a selector, such as data-id="editable-list-item" or other. 您可以在文本周围环绕一个span ,然后使用jQuery的replaceWidth()和一个公共属性作为选择器(例如data-id="editable-list-item"或其他replaceWidth() ,将元素替换为input ,反之亦然。

Similar to this: 与此类似:

 $("#refdocs_list li").on('click', 'span[data-id="editable-list-item"]', function() { var $input = $('<input type="text" data-id="editable-list-item">'); $input.val($(this).html()); $(this).replaceWith($input); $input.focus(); }); $("#refdocs_list li").on('focusout', 'input[data-id="editable-list-item"]', function() { var $span = $('<span data-id="editable-list-item">'); $span.html($(this).val()); $(this).replaceWith($span); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="refdocs_list" style="list-style-type: none; margin: 0; padding: 3px 0px 3px 3px;"> <li><span data-id="editable-list-item">test1</span></li> <li><span data-id="editable-list-item">test2</span></li> <li><span data-id="editable-list-item">test3</span></li> </ul> 

Can something like this work for you ? 这样的事情可以为您工作吗?

 <ol contenteditable="true"> <li> Line 1 </li> <li> Line 2 </li> </ol> 

Here is a simple, working solution. 这是一个简单有效的解决方案。 It is not ideal, but should be a good starting point. 这不是理想的,但应该是一个很好的起点。

 $('li').on('click',function() { $(this).children('.value').addClass('hidden'); $(this).children('.edit').removeClass('hidden'); }); $('.edit').on('change',function() { $(this).siblings('.value').html($(this).val()); $('.edit').addClass('hidden'); $('.value').removeClass('hidden'); }); 
 .hidden { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li> <span class="value">Value 1</span> <input class="edit hidden" type="text" value="Value 1" /> </li> <li> <span class="value">Value 2</span> <input class="edit hidden" type="text" value="Value 2" /> </li> <li> <span class="value">Value 3</span> <input class="edit hidden" type="text" value="Value 3" /> </li> </ul> 

See Below Example : 参见以下示例:

 $("li").click(function(){ $(this).html('<li><input type="text" value="'+ $(this).text() +'"></li>').focus() }); $("#refdocs_list li").focusout(function(){ }); 
 input[type='text']{ width:100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="field_outline" style="background: #FFF; min-height: 75px; max-height: 300px; overflow-y: auto;"> <ul id="refdocs_list" style="list-style-type: none; margin: 0; padding: 3px 0px 3px 3px;"> <li>test1</li> <li>test2</li> <li>test3</li> </ul> </div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM