使用Microsoft文档中的示例,我试图以编程方式将焦点设置为输入元素。
不幸的是,该示例使用了标准,<input type="text">
而我想将其用于InputText
element。
Microsoft示例使用扩展方法,该方法采用ElementReference
:
public static Task Focus(this ElementReference elementRef, IJSRuntime jsRuntime)
{
return jsRuntime.InvokeAsync<object>(
"exampleJsFunctions.focusElement", elementRef);
}
使用an InputText
,我看不到获得这种a的方法ElementReference
。
提供我自己的Focus()
重载,InputText
而不是经过编译,但未显示任何视觉效果。因此,我一无所知。
如何以编程方式将焦点设置为InputText
元素?
您可以在其中添加id
参数,InputText
并修改您的Focus
方法和JavaScript代码。
public async Task Focus(string elementId)
{
await JSRuntime.InvokeVoidAsync("exampleJsFunctions.focusElement", elementId);
}
focusElement: function (id) {
const element = document.getElementById(id);
element.focus();
}
注意:这不是适当的解决方案,而是更多解决方法,但是Blazor似乎不直接支持它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句