【问题标题】:Removing highlighting of misspelled words when contenteditable="false"当 contenteditable="false" 时删除拼写错误单词的突出显示
【发布时间】:2016-12-29 17:54:06
【问题描述】:

var editBtn = document.querySelector("button#edit"),
  editable = editBtn.previousElementSibling,
  saveBtn = editBtn.nextElementSibling;

editBtn.addEventListener('click', startEdit, false);
saveBtn.addEventListener('click', endEdit, false);

function startEdit() {
  editable.setAttribute("contenteditable", true);
  editable.focus();
}

function endEdit() {
  editable.setAttribute("contenteditable", false);
  // even tried
  // editable.removeAttribute("contenteditable");
}
body {
  background-color: #ccc;
}
p[contenteditable="true"] {
  font-family: "Arial", "Georgia", "Calibri";
  background-color: #fff;
  font-size: 14px;
  padding: 4px;
  color: #424245;
  border: 1px solid #C7C6CD;
}
<p>click edit, type some mispelled words, click save and highlighting remains</p>
<button id="edit">edit</button>
<button>save</button>

我有一个在 &lt;p&gt;&lt;/p&gt; 元素上设置 contenteditable="true" 的功能应用程序 当单击编辑按钮时,按下 ENTER 键时将其设置为 false

现在按下 ENTER 键并在元素上设置 contenteditable="false" 后,即使现在该元素不再可编辑,突出显示的所有拼写错误的单词仍然突出显示。

在这种情况下,有没有办法消除拼写错误单词的突出显示。

我在编辑器中运行代码 sn-p 时遇到问题,所以如果有任何问题,请告诉我。

【问题讨论】:

标签: javascript html contenteditable


【解决方案1】:

最简单的方法可能是自己覆盖内容:

var html = editable.innerHTML;
editable.innerHTML = "";
editable.innerHTML = html;

不幸的是,必须先清空内容。
只是 editable.innerHTML = editable.innerHTML; 似乎不起作用。

var editBtn = document.querySelector("button#edit"),
    editable = editBtn.previousElementSibling,
    saveBtn = editBtn.nextElementSibling;

editBtn.addEventListener('click', startEdit, false);
saveBtn.addEventListener('click', endEdit, false);

function startEdit() {
  editable.setAttribute("contenteditable", true);
  editable.focus();
}

function endEdit() {
  editable.setAttribute("contenteditable", false);
  var html = editable.innerHTML;
  editable.innerHTML = "";
  editable.innerHTML = html;
}
body {
  background-color: #ccc;
}
p[contenteditable="true"] {
  font-family: "Arial", "Georgia", "Calibri";
  background-color: #fff;
  font-size: 14px;
  padding: 4px;
  color: #424245;
  border: 1px solid #C7C6CD;
}
<p>click edit, type some mispelled words, click save and highlighting remains</p>
<button id="edit">edit</button>
<button>save</button>

【讨论】:

    猜你喜欢
    • 2012-07-02
    • 2018-11-25
    • 1970-01-01
    • 2020-11-12
    • 2013-08-25
    • 2015-05-06
    • 2010-10-16
    • 2012-07-30
    • 2018-03-31
    相关资源
    最近更新 更多