javascript - Jquery重命名标签内的内容并确认

标签 javascript jquery onclick textarea

用户应该能够更改名称,然后确认更改。我无法使用此代码存档它,因为当我单击确认时,它会像以前一样返回。 我缺少什么?

有没有更好的方法将它们组合在一起(我确信有一个)?

请查看演示,您还可以在其中看到changeElementType函数

http://jsfiddle.net/dLk6E/

js:

$('.replace').on('click', function () {
    $("h2").changeElementType("textarea");
    $('.replace').hide();
    $('.confirm').show();

    //Confermation of the change
    $('.confirm').bind('click', function () {
        $('.replace').show();
        $('.confirm').hide();
        $("textarea").changeElementType("h2");
    });


    if ($('textarea:visible')) {
        $(document).keypress(function (e) {
            if (e.which == 13) {
                alert('You pressed enter!');
                $("textarea").changeElementType("h2");
                $('.replace').css('opacity', '1');
            }
        });
    }
});

最佳答案

这是您更新的代码和工作 fiddle http://jsfiddle.net/dLk6E/

(function($) {
    $.fn.changeElementType = function(newType) {
        var attrs = {};

        $.each(this[0].attributes, function(idx, attr) {
            attrs[attr.nodeName] = attr.nodeValue;
        });

        this.replaceWith(function() {
            return $("<" + newType + "/>", attrs).append($(this).contents());
        });
    }
})(jQuery);



$('.replace').on('click', function (){
    $("h2").changeElementType("textarea");
    $('.replace').hide();

    $('.confirm').show();

    //Confermation of the change
    $('.confirm').on('click', function(){
        $('.replace').show();
        $('.confirm').hide();

        // you are missing this
        $('.replaceble').html($("textarea").val());

        $("textarea").changeElementType("h2");
    });




    if ($('textarea:visible')){
         $(document).keypress(function(e) {
            if(e.which == 13) {
                alert('You pressed enter!');
                $("textarea").changeElementType("h2");
                $('.replace').css('opacity','1');
            }
        });
    }


});

已更新 jsfiddle.net/dLk6E/1

关于javascript - Jquery重命名标签内的内容并确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24954355/

相关文章:

java - 单击后更改 JMenuItem 的标题文本?

java - 安卓 : calling a user defined method causing application misbehave

javascript - 删除/隐藏表的空列,包括 <th>

javascript - jQuery - 无法将类添加到具有 onClick 事件的按钮

javascript - 如何在 rivet.js 中绑定(bind)样式属性 top 和 left

javascript - getBoundingClientRect() 如何与框大小交互?

javascript - 从 github url 中查找用户、repo 和路径

javascript - 如何在 Angular 中抑制监视语句或摘要循环

javascript - jQuery 调用 Controller 操作不符合预期

jquery - 如何关闭 JQuery Messi 弹出框