Input 使用Blazor组件在mousedown和mouseup上选择输入中的字符(突出显示文本)
与Javascript Window.GetSelection相同。基本上,我希望能够用Blazor在html输入中获取所选文本Input 使用Blazor组件在mousedown和mouseup上选择输入中的字符(突出显示文本),input,components,blazor,highlight,selectedtext,Input,Components,Blazor,Highlight,Selectedtext,与Javascript Window.GetSelection相同。基本上,我希望能够用Blazor在html输入中获取所选文本 <input type="text" value="" /> 因此,用户将执行以下操作: 变量将包含以下内容: mySelectedText = "selection is made"; Dom操作应该使用@on完成,如中所示,但在该列表中我看不到任何@onSelection 我试过了,但没
<input type="text" value="" />
因此,用户将执行以下操作:
变量将包含以下内容:
mySelectedText = "selection is made";
Dom操作应该使用@on
完成,如中所示,但在该列表中我看不到任何@onSelection
我试过了,但没有成功。
用户事件必须是鼠标从输入中选择文本,并且必须存储或显示所选文本。解决方案是将Javascript和Blazor与
@inject IJSRuntime
在Blazor组件中:
@inject IJSRuntime js
<p @onmouseup="@GetSelectedText">Make selection with mouse on here</p>
<p>You highlighted: @SelectedText</p>
@code {
public string SelectedText { get; set; }
async Task GetSelectedText()
{
SelectedText = await js.InvokeAsync<string>("getSelectedText");
}
}
这就解决了问题这是否回答了您的问题?不,@Quango,不幸的是没有。我试图将代码从链接页面复制到组件。我甚至看不到代码的作用。换句话说,我需要看到一个工作的例子或文档的方式来选择高亮度的文本。
@inject IJSRuntime js
<p @onmouseup="@GetSelectedText">Make selection with mouse on here</p>
<p>You highlighted: @SelectedText</p>
@code {
public string SelectedText { get; set; }
async Task GetSelectedText()
{
SelectedText = await js.InvokeAsync<string>("getSelectedText");
}
}
<script>
function getSelectedText() {
return window.getSelection().toString();
}
</script>