【问题标题】:Get the selected text from an input using Blazor使用 Blazor 从输入中获取选定的文本
【发布时间】:2020-05-23 10:23:01
【问题描述】:

在 Blazor 的早期版本中,有一个带有 selectionStartselectionEnd 属性的 IHtmlInputElement 接口。

谁能解释我如何使用这些从 C# 中的文本输入控件中获取选定的文本?

更新 这是我目前所拥有的。

@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;
    }
}

【问题讨论】:

    标签: blazor blazor-server-side


    【解决方案1】:
    @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;
        }
    
    

    【讨论】:

    • 是的,它有效并且是一个答案。它与我的问题有一些细微的差别。
    猜你喜欢
    • 1970-01-01
    • 2011-07-06
    • 2015-12-24
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多