首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数未更新事件值

函数未更新事件值
EN

Stack Overflow用户
提问于 2014-08-20 19:54:29
回答 1查看 32关注 0票数 0

这是我的函数

代码语言:javascript
复制
function showNotificationBar(message) {

    /*set default values*/
    duration = typeof duration !== 'undefined' ? duration : 3000;
    bgColor = typeof bgColor !== 'undefined' ? bgColor : "#CAFFC7";
    txtColor = typeof txtColor !== 'undefined' ? txtColor : "#51A427";
    height = typeof height !== 'undefined' ? height : 40;
    /*create the notification bar div if it doesn't exist*/
    if ($('#notification-bar').size() == 0) {
        var HTMLmessage = "<div class='notification-message' style='text-align:center; line-height: " + height + "px;'> " + message + " </div>";
        $('#header').prepend("<div id='notification-bar' style='display:none; width:100%; height:" + height + "px; background-color: " + bgColor + "; position: fixed; z-index: 100; color: " + txtColor + ";border-bottom: 1px solid " + txtColor + ";'>" + HTMLmessage + "</div>");
    }
    /*animate the bar*/
    $('#notification-bar').slideDown(function() {
        setTimeout(function() {
            $('#notification-bar').slideUp(function() {});
        }, duration);
    });
}

但是当我使用showNotificationBar(" hi ")时,它会显示hi,之后当我使用showNotificationBar("hi2")时,它会再次显示hi,请帮助我

EN

回答 1

Stack Overflow用户

发布于 2014-08-20 19:57:22

这可能是因为您正在使用第一个文本创建元素。您必须将其创建为空,并在每次设置文本:

代码语言:javascript
复制
function showNotificationBar(message) {

    /*set default values*/
    duration = typeof duration !== 'undefined' ? duration : 3000;
    bgColor = typeof bgColor !== 'undefined' ? bgColor : "#CAFFC7";
    txtColor = typeof txtColor !== 'undefined' ? txtColor : "#51A427";
    height = typeof height !== 'undefined' ? height : 40;

    /*create the notification bar div if it doesn't exist*/
    if ($('#notification-bar').size() == 0) {
        var HTMLmessage = "<div class='notification-message' style='text-align:center; line-height: " + height + "px;'></div>";
        $('#header').prepend("<div id='notification-bar' style='display:none; width:100%; height:" + height + "px; background-color: " + bgColor + "; position: fixed; z-index: 100; color: " + txtColor + ";border-bottom: 1px solid " + txtColor + ";'>" + HTMLmessage + "</div>");
    }

    /* add/update the text message */
    $(".notification-message").text(message);

    /*animate the bar*/
    $('#notification-bar').slideDown(function() {
        setTimeout(function() {
            $('#notification-bar').slideUp(function() {});
        }, duration);
    });
}

请注意,它创建的.notification-message为空,然后添加文本消息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25404046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档