要获取按钮的innerText,你可以使用以下方法:
通过JSInterop可以调用JavaScript函数。在你的Blazor组件中,添加以下代码:
@inject IJSRuntime JSRuntime
然后,在需要获取按钮innerText的方法中,添加以下代码:
string innerText = await JSRuntime.InvokeAsync
其中,“getButtonText”是你要在JavaScript中调用的函数名,而“buttonRef”是你已在Blazor组件中定义的Button标签的引用。
接下来,你需要在Blazor组件中使用组件调用JS函数:
在Blazor组件中添加以下代码:
然后,定义一个方法GetButtonText,该方法使用EventCallback来获取按钮的innerText:
async Task GetButtonText()
{
string innerText = string.Empty;
await buttonRef.Current.InvokeVoidAsync("focus");
await buttonRef.Current.InvokeVoidAsync("blur");
await buttonRef.Current.InvokeAsync
在上面的代码中,当用户单击按钮时,将执行GetButtonText方法。该方法使用JS函数“focus”和“blur”来设置按钮焦点,然后使用Task来获取按钮的innerText。最后,将innerText返回给调用者。