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>
我有一个功能应用程序,可以在
contenteditable="true"
元素上设置<p></p>
单击编辑按钮时,然后按ENTER键将其设置为false
。现在,在按下ENTER键并在元素上设置了
contenteditable="false"
之后,即使现在该元素不再可编辑,突出显示的所有拼写错误的单词仍会突出显示。在这种情况下,有没有办法消除拼写错误的单词的突出显示。
我在编辑器中运行代码片段时遇到问题,所以如果有任何问题,请告诉我。
最佳答案
最简单的方法可能就是用自身覆盖内容:
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>