我需要通过内联编辑来更新段落的文本

I need to update the text of a paragraph by inline edit

本文关键字:更新 段落 文本 编辑      更新时间:2023-09-26

我试过这个

HTML

<div>
    <h1>Some text here</h1>
    <p>Text area text here</p>
</div>

jQuery

$(function(){
    $('p').on('click', function(e){
        e.preventDefault();
        var txt = $(this).text();
        $(this).parent().append('<textarea>' + txt + '</textarea>');
        $(this).remove();
    });
});

我需要通过内联编辑来更新段落的文本。我需要用在那里键入的新文本来支持该段落,并在有人点击文本区域外时删除文本区域。

感谢

以下是您的fiddle的更新http://jsfiddle.net/99pxz8et/2/

您需要的是监听新文本区域的更改事件,然后更新它

    $area.one('focusout', function() {
        $p.show();            
        $p.text($area.val());
        $area.remove()                                
    });

编辑:将.on()更改为.one()