简体   繁体   English

如何在页面加载时淡出HtmlHelper呈现的用户通知?

[英]How can I fade out a user notification rendered by HtmlHelper on page load?

I am using a HtmlHelper to display an update to a user as follows: 我正在使用HtmlHelper向用户显示更新,如下所示:

In webpage: 在网页中:

<%=Html.CourseUpdateNotification() %>

In my controller: 在我的控制器中:

public ActionResult UpdateOnceOnlyCourseRecord(some parameters)
{
    //validation code etc...
    //Save to database etc...

    TempData["CourseUpdateNotification"] = "Success";
    return RedirectToAction("ShowCourses", "Course");
}

At the moment, the notification to the user remains on the screen until the user navigates away from the page or refreshes the page (as expected). 此刻,向用户的通知将保留在屏幕上,直到用户离开页面导航或刷新页面(如预期的那样)。

The effect I would like to achieve is to display the notification but then fade it out after 3 seconds. 我想要达到的效果是显示通知,但3秒钟后将其淡出。

I tried to achieve this with the following jQuery: 我尝试使用以下jQuery实现此目标:

(NOTE - The HtmlHelper uses TagBuilder to create a div with the class attribute feedback-complete ) (注意HtmlHelper使用TagBuilder创建具有class属性feedback-completediv

$(document).ready(function() {
    $(function() {
        if ($('div.feedback-complete').length > 0) //Check if exists
        {
        setTimeout(function() { $('div.feedback-complete').fadeOut(); }, 3000);
        }
    });
});

Unfortunately this does not work for me and I cannot work out why. 不幸的是,这对我不起作用,我也无法找出原因。 I have tried a few variations, including $(window).load , etc. but to no avail. 我尝试了一些变体,包括$(window).load等,但是没有用。

Am I missing something more fundamental regarding HtmlHelper and how it can be accessed after the page load? 我是否缺少有关HtmlHelper的更基本的知识,以及页面加载后如何访问它?

Insights always appreciated. 见解总是赞赏。

Try taking out the additional $(function() { lines. 尝试取出其他$(function() {行。

Eg 例如

$(document).ready(function() {
    if ($('div.feedback-complete').length > 0) //Check if exists
    {
        setTimeout(function() { $('div.feedback-complete').fadeOut(); }, 3000);
    }
});

HTHs, HTH,
Charles 查尔斯

EDIT: Ok, if that doesn't work try this. 编辑:好的,如果那不起作用,请尝试此。

setTimeout(function() { jQuery('div.feedback-complete').fadeOut(); }, 3000);

If that doesn't work then you need to find out where the issue it. 如果那不起作用,那么您需要找出问题所在。 I would suggest strip it back to basics and moving from there. 我建议将其还原为基础知识并从那里开始。

So try this first: 因此,请首先尝试:

setTimeout("alert('I am displayed after 3 seconds!')", 3000);

If that works as expected, try this: 如果可以正常工作,请尝试以下操作:

setTimeout(function () { alert('I am displayed after 3 seconds!'); }, 3000);

Or this: 或这个:

setTimeout("jQuery('div.feedback-complete').fadeOut()", 3000);

Etc etc

As an alternative, if you are using jQuery 1.4 you could use the new .delay() method. 或者,如果您使用的是jQuery 1.4,则可以使用新的.delay()方法。

I haven't tried it at all myself but I think might be able to do something like this. 我自己还没有尝试过,但是我认为也许可以做这样的事情。

$(document).ready(function() {
    $('div.feedback-complete').delay(3000).fadeOut();
});

HTHs, HTH,
Charles 查尔斯

After some research I think the issue was how the return string from my HtmlHelper was formatted. 经过一些研究,我认为问题是HtmlHelper的返回字符串是如何格式化的。

Initially I had this in the HtmlHelper : 最初,我在HtmlHelper

    public static class NotificationHelper
    {
        public static string PasswordNotification(this HtmlHelper html)
        {
        //Other code...
        return "<div class=feedback-complete><img src=/Images/SuccessTick.png class=notification-icon alt=success>Your password has been succesfully saved</div>";

Which rendered like this in Html (note the lack of apostrophes around the class name): 在HTML中这样渲染的(请注意,类名周围没有撇号):

<div class=feedback-complete><img src=/Images/SuccessTick.png class=notification-icon alt=success>Your password has been succesfully saved</div>

Instead, I changed the HtmlHelper to format the string as follows: 相反,我更改了HtmlHelper来格式化字符串,如下所示:

    public static class NotificationHelper
    {
        public static string PasswordNotification(this HtmlHelper html)
        {
        //Other code...
        return String.Format("<div class='{0}'></label><img src=/Images/SuccessTick.png class='{1}'alt=success>Your password has been succesfully saved</div>", "feedback-complete", "notification-icon");

This then rendered Html that had ' around the class name... 然后呈现的Html在类名周围带有' ...

<div class='feedback-complete'><img src=/Images/SuccessTick.png class=notification-icon alt=success>Your password has been succesfully saved</div>

Once I changed the above I got the fade-out effect I was looking for after a 3 second delay using the following jQuery (Hat tip to Charlino ) 一旦更改了上面的内容,我得到了淡出效果,我一直在使用以下jQuery等待3秒的延迟(向Charlino提示的提示)

setTimeout("jQuery('div.feedback-complete').fadeOut()", 3000);

Finally, I find it interesting that the the lack of apostrophes around the class name did not stop the CSS from rendering the Html correctly. 最后,我发现有趣的是,类名周围缺少撇号并没有阻止CSS正确呈现HTML。 I (naively) assumed at the outset that the fact that the html fragment rendered correctly without the apostrophes meant that this couldn't be the issue of why the jQuery fadeout effect wasn't working for me... 我(天真的)一开始就假设html片段正确呈现而没有撇号的事实意味着这不可能是为什么jQuery淡出效果对我不起作用的问题...

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

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