正在读取this Webkit文档,它说与contenteditable一起使用时,beforeinput事件是可取消的。但是我在chrome浏览器上运行文档中的代码,因此该事件无法取消。
我误会了吗?如何使beforeinput事件可取消?
<body onload="setup()">
<div id="editor" contenteditable>
<p>This is some regular content.</p>
<p>This text is fully editable.</p>
<div class="quote" style="background-color: #EFFEFE;">
<p>This is some quoted content.</p>
<p>You can only change the format of this text.</p>
</div>
<p>This is some more regular content.</p>
<p>This text is also fully editable.</p>
</div>
</body>
<script>
function setup()
{
editor.addEventListener("beforeinput", event =>
{
if (event.inputType.match(/^format/))
return;
for (let staticRange of event.getTargetRanges())
{
if (nodeIsInsideQuote(staticRange.startContainer)
|| nodeIsInsideQuote(staticRange.endContainer))
{
event.preventDefault();
return;
}
}
});
function nodeIsInsideQuote(node)
{
let currentElement = node.nodeType == Node.ELEMENT_NODE ? node : node.parentElement;
while (currentElement)
{
if (currentElement.classList.contains("quote"))
return true;
currentElement = currentElement.parentElement;
}
return false;
}
}
</script>