简体   繁体   English

添加然后删除<p>

[英]Add and then remove <p>

I want add <p> with text when my form have some error. 我想在表单出现错误时在文本中添加<p> I write some js code, but it does not work now. 我写了一些js代码,但现在不起作用。 My question is: how can i add text, if i have error, show it 1800 ms, add then remove this text? 我的问题是:如何添加文本,如果出现错误,请显示1800毫秒,然后添加然后删除此文本?

$(document).ready(function () {
    ........................
        error: function () {
            $('form').append("<p class='er-msg'></p>").text('Maximum upload size 50MB. Please try again');
            $('.er-msg').animate({ opacity: 0} , 1800);
            $('.er-msg').remove();   //don't work 
        }
    };
   ....................
});

I hope someone help me. 我希望有人帮助我。

Now your animation takes 1800ms and, from what I understand, you want a delay of that time before you hide the message. 现在,您的动画耗时1800毫秒,据我了解,您希望在隐藏消息之前延迟一段时间。 So you should start with a setTimeout like this 所以你应该从这样的setTimeout开始

setTimeout(function () {
    $('.er-ms').animate({ opacity: 0} , 1800);
}, 1800);

jQuery animate takes a 3rd parameter, which is a function that will be called after the animation is over. jQuery animate具有第3个参数,该函数将在动画结束后调用。 Then you can add this, to make the message disappear. 然后,您可以添加它,使消息消失。

setTimeout(function () {
    $('.er-ms').animate({ opacity: 0} , 1800, function () {
        $(this).hide().remove();
    });
}, 1800);

When you put this in the error callback, after the append line, and get rid of the last two, you should be good to go. 将其放在error回调中的追加行之后,并去除最后两个,您应该很好。

Another way to do is it to create the <p> and set it to display: none then you can just toggle it as needed. 另一种方法是创建<p>并将其设置为display: none则可以根据需要进行切换。

html: HTML:

<p class='er-msg'>Maximum upload size 50MB. Please try again</p>

CSS: CSS:

.er-msg {
  display: none
}

jQuery: jQuery的:

error: function () {
            $('.er-msg').fadeIn("slow");
            setTimeout(function(){
               $('.er-msg').fadeOut("slow");
            }, 1800);
        }

As a personal suggestion, I would make the timer slightly higher to accommodate people that don't read fast. 作为个人建议,我可以将计时器调高一些,以适应那些阅读速度不快的人。 This way your error message is effective for anyone that happens to see it. 这样,您的错误消息对碰巧看到它的任何人都有效。

JSFIDDLE DEMO JSFIDDLE演示

Is the actual append even happening? 实际的附录是否还在发生?

I suspect it is not because you are appending <p> dynamically. 我怀疑不是因为您正在动态添加<p>

In order to successfully bind to this, you will be need to target its wrapper. 为了成功绑定到此,您将需要定位其包装器。

for example: 例如:

$('form').on(eventname, targetElement, function(){
 ....the behavior you want
});

you can check out this blog post for more info: http://blog.ning-xia.com/accessing-dynamically-created-elements-with-jquery/ 您可以查看此博客文章以了解更多信息: http : //blog.ning-xia.com/accessing-dynamically-created-elements-with-jquery/

The append of <p> is not working that's why you can't remove it. <p>的附录不起作用,这就是为什么您无法删除它的原因。 Try it this way: 尝试这种方式:

   $(document).ready(function () {
        ........................
            error: function () {
                $('form').append("<p class='er-msg'></p>");
                $('.er-ms').text('Maximum upload size 50MB. Please try again');
                $('.er-ms').animate({ opacity: 0} , 1800);
                $('.er-ms').remove();   
            }
        };
       ....................
    });

Simple solution 简单的解决方案

Here is an example how you could do this: 这是一个示例,您如何执行此操作:

var errorMessage = 'test message';
$('#question-header').append(
    $(
        '<p class="er-msg">' + 
             errorMessage  +
        '</p>'
    ).animate(
        {opacity: 0},
        1800,
        function(){
            $(this).remove();
        }
    )
);

You can call a function inside animate that runs after animation is complete. 您可以在动画中调用在动画完成后运行的函数。 Try to run this on this page in your console. 尝试在控制台的此页面上运行它。 Note that with this multiple error can be thrown in different orders and they will all show the correct behavior. 请注意,使用此错误可以以不同顺序引发,并且它们都将显示正确的行为。 You couple the animation and the removal to all your unique error messages in a simple way. 您可以通过简单的方式将动画和删除内容耦合到所有唯一的错误消息。

Your code does not work because text should be called on that created element not the element your appending it to. 您的代码无法正常工作,因为应该在创建的元素上调用文本,而不是在添加元素的文本上调用文本。

Proper way: 合适的方式:

$('form').append($('<p class="er-msg"></p>').text('Maximum uplo...'));

But I think the example above is a lot more readable, abstract and more performant. 但是我认为上面的示例更具可读性,抽象性和高性能。

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

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