6

我在我的 contenteditable div 中有这个。每当我键入#something,然后键入空格时,我想立即在该 div 中加粗该单词。

例如

这是我的信息。#哈哈。我想加粗标签。

下面是我的代码

<div id="message" name="message" contenteditable="true"></div>

$('#message').keyup(function(e){

  var len = $(this).val().length;
  if ($(this).val().substring(length - 1, 1) == '#') {

  }

  //detect space
  if(e.keyCode == 32){

  }
});

我正在使用 jquery。我该怎么做呢?

4

2 回答 2

2

使用contenteditable = "true"可能很棘手,但这是一个可能的解决方案:

当单词前面有#时,文本为粗体

  • 示例:你好#world,这是一个#sample

HTML:

<div id="divEditable" contenteditable="true"></div>

JavaScript:jsbin.com/zisote

我们将拆分代码,但实际上它被包装在一个IIFE中

/*** Cached private variables ***/
var _break = /<br\s?\/?>$/g,
    _rxword = /(#[\w]+)$/gm,
    _rxboldDown = /<\/b>$/gm,
    _rxboldUp = /<\/b>(?!<br\s?\/?>)([\w]+)(?:<br\s?\/?>)?$/,
    _rxline = /(<br\s?\/?>)+(?!<b>)(<\/b>$)+/g;
/*** Handles the event when a key is pressed ***/
$(document).on("keydown.editable", '.divEditable', function (e) {
  //fixes firefox NodeContent which ends with <br>
  var html = this.innerHTML.replace(_break, ""),
      key = (e.which || e.keyCode),
      dom = $(this);

  //space key was pressed
  if (key == 32 || key == 13) {
    //finds the # followed by a word
    if (_rxword.test(dom.text()))
      dom.data("_newText", html.replace(_rxword, "<b>$1</b>&nbsp;"));
    //finds the end of bold text
    if (_rxboldDown.test(html))
      dom.data("_newText", html + "&nbsp;");
  }
  //prevents the bold NodeContent to be cached
  dom.attr("contenteditable", false).attr("contenteditable", true);
});
/*** Handles the event when the key is released ***/
$(document).on("keyup.editable", '.divEditable', function (e) {
  var dom = $(this),
      newText = dom.data("_newText"),
      innerHtml = this.innerHTML,
      html;

  //resets the NodeContent
  if (!dom.text().length || innerHtml == '<br>') {
    dom.empty();
    return false;
  }

  //fixes firefox issue when text must follow with bold
  if (!newText && _rxboldUp.test(innerHtml))
    newText = innerHtml.replace(_rxboldUp, "$1</b>");

  //fixes firefox issue when space key is rendered as <br>
  if (!newText && _rxline.test(innerHtml)) html = innerHtml;
  else if (newText && _rxline.test(newText)) html = newText;

  if (html) newText = html.replace(_rxline, "$2$1");

  if (newText && newText.length) {
    dom.html(newText).removeData("_newText");
    placeCaretAtEnd(this);
  }
});
/*** Sets the caret position at end of NodeContent ***/
function placeCaretAtEnd (dom) {
  var range, sel;
  dom.focus();
  if (typeof window.getSelection != "undefined"
  && typeof document.createRange != "undefined") {
      range = document.createRange();
      range.selectNodeContents(dom);
      range.collapse(false);
      sel = window.getSelection();
      sel.removeAllRanges();
      sel.addRange(range);
  } else if (typeof document.body.createTextRange != "undefined") {
      //this handles the text selection in IE
      range = document.body.createTextRange();
      range.moveToElementText(dom);
      range.collapse(false);
      range.select();
  }
}

您可以在此处使用实时代码:jsbin.com/zisote

于 2013-06-19T15:16:41.843 回答
1

这是一个示例。不过,可编辑的 div 不是一个好主意。尝试改变这一点。文本区域可能更好...

http://jsfiddle.net/blackjim/h9dck/5/

function makeBold(match, p1, p2, p3, offset, string) {
    return p1+'<b>' + p2 + '</b>'+p3;
};

$('#message').keyup(function (e) {
    var $this = $(this);

    //detect space
    if (e.keyCode == 32) {
        var innerHtml = $this.html();
        innerHtml = innerHtml.replace(/(.*[\s|($nbsp;)])(#\w+)(.*)/g, makeBold);

        if(innerHtml !== $this.html()){
            $this.html(innerHtml)
                 .focus();
        }
    }
});
于 2013-06-18T19:59:49.843 回答