【问题标题】:Jquery rename content insite a tag and confirm itJquery重命名标签内的内容并确认
【发布时间】:2014-07-25 11:06:58
【问题描述】:

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

有没有更好的方法来把它放在一起(我敢肯定有一个)?

请查看demo,您还可以在其中看到changeElementTypefunction

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');
            }
        });
    }
});

【问题讨论】:

  • 但是你没有对输入的值做任何事情
  • @mitomed 是的,注意到了!但不确定如何获得此值
  • 我认为你的代码没问题,只是你应该对输入的值做一些事情,比如jsfiddle.net/7gXz7
  • @mitomed 看起来不错!!!你知道如何设置限制或其他东西吗?但是感谢这个
  • 什么限制,也许可以编辑问题并更新你真正想要实现的目标

标签: javascript jquery onclick textarea


【解决方案1】:

这是您的更新代码和工作小提琴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

【讨论】:

  • 它对我有用,你在这里的小提琴上检查了吗jsfiddle.net/dLk6E
  • 不要在 textare 上输入新文本然后尝试确认!不工作
  • 你没有链接正确的小提琴@Ramzan Zafar,只是操作的那个
  • @mitomed 谢谢我做错了这里是更新的小提琴jsfiddle.net/dLk6E/1
【解决方案2】:

我认为您的代码是正确的,但您需要在替换它时使用您输入的值。因此,确认绑定将是这样的(获取它,然后在将其“转换”为 h2 标记之前使用它来更新 textarea。

$('.confirm').bind('click', function(){
    var valueEntered = $('textarea').val();
    $('.replace').show();
    $('.confirm').hide();
    $("textarea").html(valueEntered).changeElementType("h2");
});

您可以为此使用 .on 以及 jQuery 1.7 优于 .bind。

我建议的另一件事是,每当您遇到类似这样的事情时,只需将您想要的内容放入 google(或其他...)中,在这种情况下,“jquery 获取输入值”将得到 asw 第一个结果 @987654321 @

这样你就不会忘记它;)

更新:也许是一个小细节,但在我使用的绑定中,只需点击一次 $('textarea') 会更有效,所以它会是这样的。您可能要记住的事情(这里不是真正的问题),最好将其存储在变量中而不是多次访问 DOM。

$('.confirm').on('click', function(){
    var $textarea = $('textarea');
    $('.replace').show();
    $('.confirm').hide();
    $textarea.html($textarea.val()).changeElementType("h2");
});

jsfiddle

【讨论】:

  • 这和我的回答一样:-)
  • 实际上,我不小心更新了您的评论,是的,这很相似,但请看我对您的回答的评论。你的答案确实和我在 cmets 中的小提琴几乎一样,但后来所以没有必要否决这个:)
  • 孩子们,请停止! stackoverflow 不是博彩网站!
猜你喜欢
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 2011-10-21
  • 2015-05-02
  • 1970-01-01
  • 2018-02-12
相关资源
最近更新 更多