函数没有更新事件值

时间:2014-08-20 11:54:29

标签: javascript jquery

这是我的功能

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 请帮帮我

1 个答案:

答案 0 :(得分:0)

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

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为空,然后添加文本消息。

相关问题