blazor组建实现替换菜单,实现剪切、复制、粘贴,全选操作

<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");

 

posted @ 2023-06-14 15:39  森雾  阅读(204)  评论(0编辑  收藏  举报