开发者

Append messages different color in log_console

Can somebody help me with JavaScript. I have log console

<div id="log_console"></div>

and I need to write function which has two parameters ty开发者_如何学运维pe = {success, error} and text ="some text". I need to show each message in separated line , blue if success, red if error.

How to solve this problem ?


function log(type, text) {
    var colour = (type = 'success') ? 'blue', 'red';
    $('<p />').css('color', colour).html(text).appendTo($('#log_console'));
}


This function called onclick event should do the trick:

function appendToLog( type, text )
{
    var myLogDiv = document.getElementById("log_console");

    var myText = document.createTextNode(text + "<br/>");
    var myDiv = document.createElement("div");
    if(type=="error")
    {
        myDiv.style.color = "red";
    }
    else
    {
        myDiv.style.color = "blue";
    }

    myDiv.appendchild( myText );
    myLogDiv.appendChild( myDiv );

}



function add_message(type, text) {
    var color;
    switch(type) {
        case "success":
            color = "blue";
            break;
        case "error":
            color = "red";
            break;
        default:
            return;
    }
    var new_div = $("", { 'html': text, 'css': { 'color': color } });
    $("#log_console").append(new_div);
}

You can play with it here: http://jsfiddle.net/NdWZr/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜