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 @   森雾  阅读(213)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示