JavaScript利用DOM给页面添加内容


自定义一个log函数,输出传入函数的对象或者信息.


Log.js
// JScript source code

function
 log(category, message, object) {
    // If this category is explicitly disabled, do nothing

    if
 (log.options[category + "Disabled
"]) return
;

    // Find the container

    var
 id = category + "_log
";
    var
 c = document
.getElementById(id);

    // If there is no container, but logging in this category is enabled,

    // create the container.

    if
 (!c && log.options[category + "Enabled
"]) {
        c = document
.createElement("div
");
        c.id = id;
        c.className = "log
";
        document
.body.appendChild(c);
    }

    // If still no container, we ignore the message

    if
 (!c) return
;

    // If timestamping is enabled, add the timestamp

    if
 (log.options.timestamp)
        message = new
 Date
() + ": 
" + (message ? message : "");

    // Create a  element to hold the log entry
    var
 entry = document
.createElement("div
");
    entry.className = category + "_message
";

    if
 (message) {
        // Add the message to it

        entry.appendChild(document
.createTextNode(message));
    }

    if
 (object && typeof
 object == "object
") {
        entry.appendChild(log.makeTable(object, 0));
    }

    // Finally, add the entry to the logging container

    c.appendChild(entry);
}

// Create a table to display the properties of the specified object

log.makeTable = function
(object, level) {
    // If we've reached maximum recursion, return a Text node instead.

    if
 (level > log.options.maxRecursion)
        return
 document
.createTextNode(object.toString
());

    // Create the table we'll be returning

    var
 table = document
.createElement("table
");
    table.border = 1;

    // Add a Name|Type|Value header to the table

    var
 header = document
.createElement("tr
");
    var
 headerName = document
.createElement("th
");
    var
 headerType = document
.createElement("th
");
    var
 headerValue = document
.createElement("th
");
    headerName.appendChild(document
.createTextNode("Name
"));
    headerType.appendChild(document
.createTextNode("Type
"));
    headerValue.appendChild(document
.createTextNode("Value
"));
    header.appendChild(headerName);
    header.appendChild(headerType);
    header.appendChild(headerValue);
    table.appendChild(header);

    // Get property names of the object and sort them alphabetically

    var
 names = [];
    for
 (var
 name
 in
 object) names.push(name
);
    names.sort();

    // Now loop through those properties

    for
 (var
 i = 0; i < names.length
; i++) {
        var
 name
, value, type;
        name
 = names[i];
        try
 {
            value = object[name
];
            type = typeof
 value;
        }
        catch
 (e) { // This should not happen, but it can in Firefox

            value = "
";
            type = "unknown
";
        };

        // Skip this property if it is rejected by a filter

        if
 (log.options.filter && !log.options.filter(name
, value)) continue
;

        // Never display function source code: it takes up too much room

        if
 (type == "function
") value = "{/*source code suppressed*/}
";

        // Create a table row to display property name, type and value

        var
 row = document
.createElement("tr
");
        row.vAlign = "top
";
        var
 rowName = document
.createElement("td
");
        var
 rowType = document
.createElement("td
");
        var
 rowValue = document
.createElement("td
");
        rowName.appendChild(document
.createTextNode(name
));
        rowType.appendChild(document
.createTextNode(type));

        // For objects, recurse to display them as tables

        if
 (type == "object
")
            rowValue.appendChild(log.makeTable(value, level + 1));
        else

            rowValue.appendChild(document
.createTextNode(value));
        // Add the cells to the row, and add the row to the table

        row.appendChild(rowName);
        row.appendChild(rowType);
        row.appendChild(rowValue);

        table.appendChild(row);
    }

    // Finally, return the table.

    return
 table;
}

// Create an empty options object

log.options = {timestamp:true
};

// Utility versions of the function with hardcoded categories

log.debug = function
(message, object) { log("debug
", message, object); };
log.warn = function
(message, object) { log("warning
", message, object); };

// Uncomment the following line to convert alert() dialogs to log messages

// function alert(msg) { log("alert", msg); }





log.css
#debug_log { /* Styles for our debug message container */
    background-color: #aaa;    /* gray background */
    border: solid black 2px;   /* black border */
    overflow: auto;            /* scrollbars */
    width: 75%;                /* not as wide as full window */
    height: 300px;             /* don't take up too much vertical space */
}

#debug_log:before { /* Give our logging area a title */
    content: "Debugging Messages";
    display: block;
    text-align: center;
    font: bold 18pt sans-serif ;
}

.debug_message { /* Place a thin black line between debug messages */
    border-bottom: solid black 1px;
}


运行结果:

优质内容筛选与推荐>>
1、多版本jQuery的使用剖析
2、pods安装使用
3、iOS网络编程开发-数据加密
4、接入HTTPS协议的CAS登录配置
5、鲁大师与EVEREST哪个好?


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn