How to change/edit the text of a paragraph/div using jQuery?

可紊 提交于 2020-01-05 03:30:27

问题


Now what I'm trying to do is to change/edit the text of a paragraph and/or a div tag. Both elements are created dynamically with their own incremental id. I want the user to be able to enter a text in a textarea which would change the text of a paragraph or div. When they select the paragraph or div a textarea should appear that way users can update the text.

Problem: Since I have set the incremental ID on both elements its hard for me to get them.

How can I achieve this?

This is the code I've been working on

http://jsfiddle.net/RzvV5/93/

jQuery:

$(document).ready(function(){

var pid = 1;
$("#addP").on({
    click: function(){
        var pr = $('<p />').attr({'class':'test', 'id':'paragraph_' + pid}).text('This is a paragraph ' + pid);
        var d = $("#Test");
        var pclone = $(pr).clone();
        pclone.on({
            mouseenter: function(){    
                $(this).addClass("inside");
            },
            mouseleave: function(){                             
                $(this).removeClass("inside"); 
            },
                    });
        pclone.appendTo(d);
pid++;
    }
});
var divid = 1;
$("#addDiv").on({
    click: function(){
        var pr = $('<div />').attr({'class':'test', 'id':'div_' +     divid}).text('This is a div ' + divid);
        var d = $("#Test");
        var pclone = $(pr).clone();
        pclone.on({
            mouseenter: function(){    
                $(this).addClass("inside");
            },
            mouseleave: function(){                             
                $(this).removeClass("inside"); 
            },
                    });
        pclone.appendTo(d);
divid++;
    }
});




var div =  $('<div class="customD" id="d"></div>');
var del = $('<a href="#" class="delete" id="erase">Delete</a>');

$('#updateP').hide();
$('#updateD').hide();

var flag = false;
$("#Test").on("click", "p", function(){ 
var cur = $(this).css("background-color");


   if(cur=="rgb(255, 255, 255)") {  
       if(flag==false){
           $('#updateP').show();
            $('#updateD').show();

    $(this).css("background-color","#FDD").addClass("help insider").after(div);
flag = true;

}

}
else { 
 $('#updateP').hide();
 $('#updateD').hide();

            $(this).css("background-color","white").removeClass('help insider');
 $(div).remove();
flag = false;
}    
$(div).append(del);  


$(".delete").on("click",function(){
    $(this).parent().prev().remove();
$(this).remove();
flag = false;

});


}); 

$('#updateP').change(function(){
var a = $(this).val();
$('p').text(a);

});
id++;     
});

HTML:

 <a href="#" id="addP">P tag</a> or <a href="#" id="addDiv">Div   tag</a><br/><br/>
    <textarea id="updateP"></textarea>
    <textarea id="updateD"></textarea>

    <div id="Test"></div>

回答1:


There's a much simpler solution than the big page of code you're trying. The best way to do this is:

  • Set an event that saves the clicked div/p
  • Make a JavaScript function to display a textarea
  • Fill the textarea with the contents of the div/p
  • Add a save button that updates the clicked div/p
  • Add a close button for when they want to hide the textarea again

I've made a simple jsFiddle that illustrates this perfectly.

EDIT: I've updated the jsFiddle.




回答2:


If I get your question right and that the incremental id part for div and paragraph are the same, you may try this: add a class say div_class to each div. Then

$('.div_class').keydown(function() {//Use appropriate listener
//get the incremental_id part from the div
var incremental_id = $(this).attr('id').substring(4);

var text = $(this).text();
//Copy the text into para using appropriate function. I have assumed text will work.
$('#para_'+incremental_id).text(text);
});

I have assumed input in div and it is to be copied in para. Use according to your need. Use On/Live as needed.



来源:https://stackoverflow.com/questions/10476533/how-to-change-edit-the-text-of-a-paragraph-div-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!