使用Blazor从输入中获取所选文本 [英] Get the selected text from an input using Blazor

查看:129
本文介绍了使用Blazor从输入中获取所选文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Blazor的早期版本中,存在一个具有 selectionStart selectionEnd 属性的 IHtmlInputElement 接口.

In an early version of Blazor there was an IHtmlInputElement interface with selectionStart and selectionEnd properties.

任何人都可以解释我如何使用它们从C#中的文本输入控件中获取选定的文本吗?

Can anyone explain how I can use these to get the selected text from a text input control in C#?

更新这就是我到目前为止所拥有的.

UPDATE Here's what I have so far.

@page "/selectedtext"
@inject IJSRuntime JsRuntime

<h3>TextSelection</h3>

<input type="text" placeholder="Type here" @ref="myTextInput"/>

<button class="btn btn-primary" @onclick="@(async () => await GetSelectionStart(myTextInput))"></button>

@code {
    public ElementReference myTextInput { get; set; }

    public async Task GetSelectionStart(ElementReference element)
    {
        int pos = await JsRuntime.InvokeAsync<int>("GetSelectedStart", element);
    }
}

// myscript.js
{
    getSelectedStart : function (element) {
        return element.selectionStart;
    }
}

推荐答案

@page "/selectedtext"
@inject IJSRuntime JsRuntime

<h3>TextSelection</h3>

<input type="text" placeholder="Type here" @ref="myTextInput"/>

<button class="btn btn-primary" @onclick="@(async () => await GetSelectionStart(myTextInput))">Get Position</button>

@code {
    public ElementReference myTextInput { get; set; }

    public async Task GetSelectionStart(ElementReference element)
    {
        int pos = await JsRuntime.InvokeAsync<int>("getSelectedStart", element);
    }
}

// myscript.js
window.getSelectedStart = (element) => {
        return element.selectionStart;
    }

这篇关于使用Blazor从输入中获取所选文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆