Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
載說明:原創(chuàng)不易,未經(jīng)授權(quán),謝絕任何形式的轉(zhuǎn)載
有時候,我們希望使用 JavaScript 將文本以純文本形式粘貼到可編輯內(nèi)容元素中。
在本文中,我們將討論如何使用 JavaScript 將文本以純文本形式粘貼到可編輯內(nèi)容元素中。
我們可以通過監(jiān)聽粘貼事件并修改粘貼行為,將純文本粘貼到可編輯內(nèi)容元素中。
例如,如果我們有以下的 div 元素:
<div contenteditable>
</div>
然后,我們可以通過監(jiān)聽粘貼事件來修改粘貼行為:
// 獲取可編輯的 div 元素
const editor = document.querySelector('div');
// 監(jiān)聽粘貼事件
editor.addEventListener("paste", (e) => {
e.preventDefault(); // 阻止默認(rèn)粘貼行為
// 獲取粘貼的純文本內(nèi)容
const text = e.clipboardData.getData('text/plain');
// 將純文本插入到可編輯元素中
document.execCommand("insertHTML", false, text);
});
當(dāng)你需要在可編輯的內(nèi)容元素中以純文本形式粘貼文本時,你可以使用上述代碼來實現(xiàn)。讓我逐步解釋每一部分的作用:
1. 首先,我們通過 `document.querySelector('div')` 獲取到一個可編輯的 `<div>` 元素,這個元素將用于粘貼操作。
2. 然后,我們使用 `editor.addEventListener("paste", (e) => { ... });` 添加一個粘貼事件監(jiān)聽器。這意味著當(dāng)用戶嘗試粘貼內(nèi)容時,我們將執(zhí)行指定的操作。
3. 在事件監(jiān)聽器的函數(shù)內(nèi)部,`e` 是代表事件對象的參數(shù)。我們使用 `e.preventDefault();` 來阻止瀏覽器默認(rèn)的粘貼行為,以便我們能夠自行處理粘貼操作。
4. `e.clipboardData.getData('text/plain')` 這行代碼用于從剪貼板中獲取純文本內(nèi)容。`e.clipboardData` 是一個包含剪貼板數(shù)據(jù)的對象,我們使用 `getData('text/plain')` 方法來獲取純文本數(shù)據(jù)。
5. 最后,我們使用 `document.execCommand("insertHTML", false, text);` 將獲取到的純文本內(nèi)容插入到可編輯元素中。`insertHTML` 是一個命令,它允許我們將指定的 HTML 或文本插入到文檔中。第三個參數(shù) `text` 是要插入的內(nèi)容。
綜合起來,當(dāng)用戶在可編輯元素中粘貼內(nèi)容時,粘貼事件會觸發(fā)。代碼阻止默認(rèn)的粘貼行為,然后從剪貼板中獲取純文本數(shù)據(jù),并將它以純文本的形式插入到可編輯的 `<div>` 元素中,實現(xiàn)了將文本粘貼為純文本的效果。
通過監(jiān)聽粘貼事件并修改粘貼行為,我們可以將純文本粘貼到可編輯內(nèi)容元素中。
由于文章內(nèi)容篇幅有限,今天的內(nèi)容就分享到這里,文章結(jié)尾,我想提醒您,文章的創(chuàng)作不易,如果您喜歡我的分享,請別忘了點贊和轉(zhuǎn)發(fā),讓更多有需要的人看到。同時,如果您想獲取更多前端技術(shù)的知識,歡迎關(guān)注我,您的支持將是我分享最大的動力。我會持續(xù)輸出更多內(nèi)容,敬請期待。
、nuget 引用
Select.HtmlToPdf
2、方法
using SelectPdf;
using System.Collections.Specialized;
using System.IO;
using System.Web;
namespace BQoolCommon.Helpers.File
{
public class WebToPdf
{
public WebToPdf()
{
//SelectPdf.GlobalProperties.LicenseKey = "your-license-key";
}
/// <summary>
/// 將 Html 轉(zhuǎn)成 PDF,並儲存成檔案
/// </summary>
/// <param name="html">html</param>
/// <param name="fileName">絕對路徑</param>
public void SaveToFileByHtml(string html, string fileName)
{
var doc = SetPdfDocument(html);
doc.Save(fileName);
}
/// <summary>
/// 傳入 Url 轉(zhuǎn)成 PDF,並儲存成檔案
/// </summary>
/// <param name="url">url</param>
/// <param name="fileName">絕對路徑</param>
/// <param name="httpCookies">Cookies</param>
public void SaveToFileByUrl(string url, string fileName, NameValueCollection httpCookies)
{
var doc = SetPdfDocument(url, httpCookies);
doc.Save(fileName);
}
/// <summary>
/// 將 Html 轉(zhuǎn)成 PDF,並輸出成 byte[] 格式
/// </summary>
/// <param name="html">html</param>
/// <returns></returns>
public byte[] GetFileByteByHtml(string html)
{
var doc = SetPdfDocument(html);
return doc.Save();
}
/// <summary>
/// 傳入 Url 轉(zhuǎn)成 PDF,並輸出成 byte[] 格式
/// </summary>
/// <param name="url">url</param>
/// <param name="httpCookies">Cookies</param>
/// <returns></returns>
public byte[] GetFileByteByUrl(string url, NameValueCollection httpCookies)
{
var doc = SetPdfDocument(url, httpCookies);
return doc.Save();
}
/// <summary>
/// 將 Html 轉(zhuǎn)成 PDF,並輸出成 Stream 格式
/// </summary>
/// <param name="html">html</param>
/// <returns></returns>
public Stream GetFileStreamByHtml(string html)
{
var doc = SetPdfDocument(html);
var pdfStream = new MemoryStream();
doc.Save(pdfStream);
pdfStream.Position = 0;
return pdfStream;
}
/// <summary>
/// 傳入 Url 轉(zhuǎn)成 PDF,並輸出成 Stream 格式
/// </summary>
/// <param name="html">html</param>
/// <returns></returns>
public Stream GetFileStreamByUrl(string url, NameValueCollection httpCookies)
{
var doc = SetPdfDocument(url, httpCookies);
var pdfStream = new MemoryStream();
doc.Save(pdfStream);
pdfStream.Position = 0;
return pdfStream;
}
private PdfDocument SetPdfDocument(string html)
{
var converter = new HtmlToPdf();
converter.Options.WebPageWidth = 1200;
html = HttpUtility.HtmlDecode(html);
return converter.ConvertHtmlString(html);
}
private PdfDocument SetPdfDocument(string url, NameValueCollection httpCookies)
{
var converter = new HtmlToPdf();
converter.Options.WebPageWidth = 1200;
if (httpCookies != && httpCookies.Count != 0)
{
converter.Options.HttpCookies.Add(httpCookies);
}
return converter.ConvertUrl(url);
}
}
}
們在電影時經(jīng)常能夠看到一些炫酷的打字機效果,用于顯示一些地名或劇情簡介。我們在網(wǎng)頁中也能夠?qū)崿F(xiàn)類似的效果,目前網(wǎng)上已經(jīng)有很多打字機效果的插件,如輕量級響應(yīng)式j(luò)Query打字機特效插件和逼真的js打字機效果插件等。那么,實現(xiàn)類似的打字機效果困難嗎?其實要制作一個打字機效果非常簡單,下面我們就以純JavaScript來實現(xiàn)一個炫酷的打字機效果。
在完成這個打字機效果之前,先來體驗一下打字機的效果:
請點擊此處輸入圖片描述
在輸入框輸入你想輸入的內(nèi)容后點擊開始打印,上面的展示欄則以想打印一樣一個字一個字輸出。
首先建立顯示打字效果的容器;
請點擊此處輸入圖片描述
ID為teletype的<span>元素用來顯示打印的文字,開始是它帶有一個hidden屬性,用來阻止文字在開始時就顯示出來。
定義一個光標(biāo)元素,它用于模擬打字時閃爍的光標(biāo)效果:
請點擊此處輸入圖片描述
然后將光標(biāo)元素插入到span#teletype節(jié)點中。
請點擊此處輸入圖片描述
開始打印文字時,將span#teletype元素的hidden屬性去掉。
請點擊此處輸入圖片描述
然后通過一個計時器不斷的刷新span#teletype元素中的內(nèi)容,通過一個counter計數(shù)器來不斷累加要顯示的文字,模擬出打字的效果。
請點擊此處輸入圖片描述
完整js代碼:
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。