<div> <button @onclick="SelectAll">Select All</button> <button @onclick="Copy">Copy</button> <button @onclick="Cut">Cut</button> <button @onclick="Paste">Paste</button> </div> <textarea id="myTextArea"></textarea> @code { private ElementReference myTextArea; private void SelectAll() { JSRuntime.InvokeVoidAsync("eval", "document.getElementById('myTextArea').select()"); } private void Copy() { JSRuntime.InvokeVoidAsync("eval", "document.getElementById('myTextArea').select(); document.execCommand('copy');"); } private void Cut() { JSRuntime.InvokeVoidAsync("eval", "document.getElementById('myTextArea').select(); document.execCommand('cut');"); } private void Paste() { JSRuntime.InvokeVoidAsync("eval", "document.getElementById('myTextArea').select(); document.execCommand('paste');"); } }
补充:如果粘贴不可用时,可以替换为以下代码,获取到剪切板中的信息
var text = await jsRuntime.InvokeAsync<string>("navigator.clipboard.readText");
标签:JSRuntime,getElementById,private,myTextArea,全选,select,blazor,document,粘贴 From: https://www.cnblogs.com/LSYLY97/p/17480432.html