本文介紹了從 contentEditable div 中刪除格式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我有一個 contentEditable Div,我想刪除任何格式,尤其是復制和粘貼文本.
I have a contentEditable Div and I want remove any formatting especially for copy and paste text.
推薦答案
你試過使用innerText嗎?
Have you tried using innerText?
添加:
如果您想從粘貼到可編輯 div 的內容中去除標記,請嘗試創建臨時 div 的舊技巧 - 請參見下面的示例.
If you want to strip markup from content pasted into the editable div, try the old hack of creating a temporary div -- see example below.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Strip editable div markup</title>
<script type="text/javascript">
function strip(html) {
var tempDiv = document.createElement("DIV");
tempDiv.innerHTML = html;
return tempDiv.innerText;
}
</script>
</head>
<body>
<div id="editableDiv" contentEditable="true"></div>
<input type="button" value="press" onclick="alert(strip(document.getElementById('editableDiv').innerText));" />
</body>
</html>
這篇關于從 contentEditable div 中刪除格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!