【问题标题】:Textbox for Logmessages HTML/JS用于日志消息 HTML/JS 的文本框
【发布时间】:2016-08-31 07:18:12
【问题描述】:

在创建自己的解决方案之前,我尝试寻找已经满足我需求的东西。我有一个 node.js 服务器,多个客户端/应用程序连接到该服务器。这些客户端会将日志消息发送到我希望在面板中显示的服务器。

现在我需要一些功能来实现用于日志消息的典型多行文本框

  • 我需要能够附加日志消息,因为它们将通过 websockets 定期发送
  • 它应该自动向下滚动,除非用户选择文本或向上滚动
  • 它应该能够使用颜色和粗体/常规

我的问题:

是否已经有针对上述用例的解决方案?

【问题讨论】:

  • 类似:ta.value+=msg.body+"\n"; ta.scrollTop=9e9;

标签: javascript jquery html node.js


【解决方案1】:

我可以给你我的example吗?它曾经是 textarea,但我已将其重构为 div,几乎没有更改代码。

代码的一些亮点,可在github上获得

发送日志消息的自定义函数:

/**
 * Add a message to the gamelog
 * @param {Object} options : allows custom output
 * @param {String} options.message : the message to display
 * @param {Boolean} options.isTimed : does the message has a timestamp in front of it?
 * @param {Boolean} options.isError : is the message an error?
 * @param {Boolean} options.isNewline : start the message on a new line
 */
addMessage: function (options) {
    var instance = ns.instance,
        audio = instance.audio,
        audiofx = audio.settings.fx,
        history = this.areaMessage.html();

    // isTimed?
    options.message = options.isTimed
        ? history + this.fieldClock.val() + ': ' + options.message
        : history + options.message;

    // isNewline?
    if (options.isNewline) {
        options.message = options.message + '<br />';
    }

    // message
    this.areaMessage.html(options.message);
    this.scrollTop(this.areaMessage);

    // isError?
    if (options.isError) {
        audio.play(audiofx.error);
    }
},

滚动到顶部功能:

/**
 * Automatically scroll down (from the top)
 * @param {Object} target : jQuery object
 */
scrollTop: function (target) {
    target.scrollTop(99999);
    target.scrollTop(target.scrollTop() * 12);
}

要使用彩色消息,您应该能够使用 HTML 字符串:

log.addMessage({
    message: '<span style="color: red;">[ERROR]</span>&nbsp;',
    isNewLine: false
});

log.addMessage({
    message: 'the rest of the error message',
    isNewLine: true
});

随意使用这个想法来注册您自己的自定义消息框。

【讨论】:

  • 在 yuge 日志中可能会变慢; appendChild 比每次更新都破坏所有丰富的 html 更好......
  • 是的,你可能是对的!可以这么说,只需要这个才能在一场比赛中幸存下来。顺便说一句,我喜欢你的高尔夫版本^^
  • @TimVermaelen 在花了更多时间听取您的建议后,我注意到这并不完全是我想要的,因为我缺少一些选项,例如当有人向上滚动和格式化内容(如单个单词)时停止 autoScrollDown在文本区域内(您的建议不起作用)。我想我会创建一个赏金,并希望有人知道一个解决这个用例的库。我相信很多人已经面临类似的问题
  • 很公平,虽然我很好奇为什么 HTML 格式不起作用。
猜你喜欢
  • 2015-01-06
  • 2021-10-26
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多