简体   繁体   English

在更新段落中的文本后,单击将段落转换为文本区域

[英]On Click Paragraph to Textarea after it Update the Text in Paragraph

 $(document).ready(function() { $("body").tooltip({ selector: '[data-toggle=tooltip]' }); }); function editable_text_clicked() { var div_text = $(this).html(); var text_div = $("<textarea />"); text_div.val(div_text); $(this).replaceWith(text_div); text_div.focus(); text_div.blur(text_divBlurred); } function blurred(argument) { var html = $(this).val(); var viewableText = $(".editable_text p"); viewableText.html(html); $(this).replaceWith(viewableText); // setup the click event for this new div viewableText.click(editable_text_clicked); } $(document).ready(function() { $(".btn").click( function(){ $("editable_text").(editable_text_clicked); }) }); 
 p, textarea{ margin: 20px; padding: 15px; border: 1px solid #ccc; width: 300px; } .btns{ width:300px; margin:20px; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <!-- Google JS --> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <div class="editable_text"> <p> Lorem Ipsum Content Comes here... </p> <div class="btns"> <a href="#edit" class="btn btn-warning pull-left" data-toggle="tooltip" data-placement="bottom" title="Click to Edit"><i class="glyphicon glyphicon-pencil"></i></a> <a href="#update" class="btn btn-success pull-right" data-toggle="tooltip" data-placement="top" title="Click to Update"><i class="glyphicon glyphicon-ok"></i></a> </div> </div> 
I'm poor in JS, but sill wanted to learn it, my task for today is to create a To Do List , So here's what I wanted Exactly. 我不懂JS,但仍然想学习它,今天的任务是创建“待办事项列表” ,所以这正是我想要的。 When I click on pencil icon, the p in .editable_text should edit, and after editing when I click on okay the text should be appended to the p in .editable_text 当我单击铅笔图标时, .editable_textp应该进行编辑,当我单击确定时,在编辑之后,应该将文本附加到.editable_textp

here's the fiddle Link, https://jsfiddle.net/zeasts/nfL7qcak/8/ 这是小提琴链接, https://jsfiddle.net/zeasts/nfL7qcak/8/

Update the code as mentioned below. 如下所述更新代码。 as user will click on edit button - textarea will show with content and as user will click on save p tag will display with content It will work, You can see here - https://jsfiddle.net/mf8wp3wf/ : 当用户单击编辑按钮时-文本区域将显示内容,并且当用户单击保存p标签将显示内容。它将起作用,您可以在此处看到-https : //jsfiddle.net/mf8wp3wf/

<div class="editable_text">
<div id="dynamic-div">
  <p id="content-data">
    Lorem Ipsum Content Comes here...
  </p>
</div>

<div class="btns">
    <a href="#edit" class="btn edit-text btn-warning pull-left" data-toggle="tooltip" data-placement="bottom" title="Click to Edit"><i class="glyphicon glyphicon-pencil"></i></a>
    <a href="#update" class="btn update-text btn-success pull-right" data-toggle="tooltip" data-placement="top" title="Click to Update"><i class="glyphicon glyphicon-ok"></i></a>
  </div>
</div>


$(document).ready(function() {
  $("body").tooltip({ selector: '[data-toggle=tooltip]' });

  $(".edit-text").click( function(){ 
     var content = $("#content-data").html();
     $("#dynamic-div").html("<textarea id='content-area'>"+content+"</textarea>");
  });

  $(".update-text").click( function(){ 
    var text_date = $("#content-area").val();
    $("#dynamic-div").html("<p id='content-data'>"+text_date+"</p>");
  });


});

a fast solution without functions. 没有功能的快速解决方案。 Add blurring and disable buttons onclick events. 添加模糊和禁用按钮onclick事件。

 $(document).ready(function() { $("body").tooltip({ selector: '[data-toggle=tooltip]' }); }); $(document).ready(function() { $("#edit").click( function(){ if($('.editable_text').css('display')=='block'){ var text=$('.editable_text').text(); $('.editable_text').css('display','none'); $('#textarea_text').css('display','block').val(text).focus(); } }) $("#update").click( function(){ if($('#textarea_text').css('display')=='block'){ var val=$('#textarea_text').val(); $('.editable_text').css('display','block').text(val); $('#textarea_text').css('display','none'); } }) }); 
 p, textarea{ margin: 20px; padding: 15px; border: 1px solid #ccc; width: 300px; } .btns{ width:300px; margin:20px; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <!-- Google JS --> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <div> <p class="editable_text">Lorem Ipsum Content Comes here... </p> <textarea id="textarea_text" style="display:none"></textarea> </div> <div class="btns"> <a id="edit" href="#edit" class="btn btn-warning pull-left" data-toggle="tooltip" data-placement="bottom" title="Click to Edit"><i class="glyphicon glyphicon-pencil"></i></a> <a id="update" href="#update" class="btn btn-success pull-right" data-toggle="tooltip" data-placement="top" title="Click to Update"><i class="glyphicon glyphicon-ok"></i></a> </div> 

You have some errors in your code check your console. 您的代码中有一些错误,请检查控制台。

As for the edits, this is how you can do it: 至于编辑,这是您可以执行的操作:

$("[href='#edit']").click(function(){
    $(".editable_text").attr("contenteditable", "true").focus();
});

$("[href='#update']").click(function(){
    $(".editable_text").attr("contenteditable", "false");
});

Here is ur updated JSFiddle 这是您更新的JSFiddle

In my demo I identified the elements by their href attribute. 在我的演示中,我通过它们的href属性标识了元素。 You can always add a new class to them or use unique IDs. 您始终可以向它们添加新的类或使用唯一的ID。

Also note that when you want to refer to an element by class, dont forget the dot infront $(".editable_text") 另请注意,当您要按类引用元素时,请不要忘记前面的 $(".editable_text")

it can be very simple, check this out: 这可能非常简单,请查看以下内容:

 $(document).ready(function() { $("body").tooltip({ selector: '[data-toggle=tooltip]' }); }); $('#update').on('click', function(){ $('#result').append('<li>' + $('#myText').val() + '</li>'); $('#myText').val("").attr("disabled", "disabled"); }); $('#edit').on('mouseup', function(){ console.log( $('#myText') ); $('#myText').attr("disabled", false).focus().select(); }); 
 input{ margin: 20px; padding: 15px; border: 1px solid #ccc; width: 300px; } .btns{ width:300px; margin:20px; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <!-- Google JS --> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <div class="editable_text"> <input type='text' disabled id="myText" placeholder="Lorem Ipsum Content Comes here..."/> <div class="btns"> <a href="#edit" id="edit" class="btn btn-warning pull-left" data-toggle="tooltip" data-placement="bottom" title="Click to Edit"><i class="glyphicon glyphicon-pencil"></i></a> <a href="#update" id="update" class="btn btn-success pull-right" data-toggle="tooltip" data-placement="top" title="Click to Update"><i class="glyphicon glyphicon-ok"></i></a> </div> <br><br><br><br> TODO LIST: <div id="result"></div> </div> 

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

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