如何防止 CSS 粘贴到 contenteditable div 中

Som*_*son 2 html javascript

我有一个contenteditableDIV 元素,我不想粘贴 CSS,基本上只是它只粘贴到innerText,而不是innerHTML。我没有使用任何库,只是纯粹的 Javascript。目前,如果我从网站复制一些 CSS 文本,然后将其粘贴到我的 div 元素中,样式也会随之而来。我想要它,这样如果我要粘贴它,CSS 就不会出现,或者使 CSS 被oninput我为 div 元素的类放置的事件中的字体/颜色覆盖。那么,我该怎么做呢?

DIV 元素的 CSS:

.mystyle {
    border:4px solid gray;
    background-color: #00000070;
    color: white;
    font-family: 'Courier New', Courier, monospace;
    width: 1245px;
    height: 200px;
    padding: 10px;
    font-size: 14px;
    text-shadow: 3px 3px #3f3f3f;
    text-align: justify;
  }
Run Code Online (Sandbox Code Playgroud)

HTML 片段:

    <div class="mystyle" contenteditable="true" id="input" 
    oninput='//what to put here?'></div>
Run Code Online (Sandbox Code Playgroud)

小智 6

尝试这个:contenteditable="plaintext-only"
并不适用于所有浏览器,但适用于 Chrome - 唯一重要的浏览器......:)

发现于:contenteditable - HTML:超文本标记语言| MDN的“浏览器兼容性”部分。