一般的文本復制只是復制文本而不重視樣式。有什么方法可以復制文本而不丟失它的固有屬性,如字體系列、字體大小等。javascript 函式會很有幫助。謝謝。
uj5u.com熱心網友回復:
在 HTML DOM 樹上選擇正確的元素,右鍵單擊它并選擇“復制”>“復制樣式”。
來源:https : //getcssscan.com/blog/how-to-inspect-copy-element-css
uj5u.com熱心網友回復:
您可以使用Clipboard.write()text 和 html 部分:
TS游樂場
// This will return the raw HTML, but perhaps you want to do something different,
// for example: recursively embed computed styles:
// https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
function serializeElement (element: Element): string {
return element.outerHTML;
}
async function copyTextWithStyles (element: Element): Promise<void> {
const html = serializeElement(element);
const htmlBlob = new Blob([html], {type: 'text/html'});
const text = element.textContent ?? '';
const textBlob = new Blob([text], {type: 'text/plain'});
const clipboardItem = new ClipboardItem({
[htmlBlob.type]: htmlBlob,
[textBlob.type]: textBlob,
});
return navigator.clipboard.write([clipboardItem]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/393870.html
標籤:javascript html css
