最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

event handling - In my Blazor Server side app: How can I fire an action when anywhere on the razor page's body is clicke

programmeradmin1浏览0评论

In my blazor server side app I want to listen to the mouseclick event on a razor page, and when mouseclick occurs (except on a button), I want to run a method. I have tried following with JavaScript but didn't worked for me:

in my _host.cshtml added following script:

window.addDocumentClickListener = () => {
  document.addEventListener('click', function (e) {
    if (e.target.tagName.toLowerCase() !== 'button') {
      DotNet.invokeMethodAsync('MyProjectName', 'HandleMouseClick');
    }
  });
};

in my Razor page:

@inject IJSRuntime JS
    ...
    ...
@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender )
        {
            await JS.InvokeVoidAsync("addDocumentClickListener");
        }

    }
    [JSInvokable]
    public void HandleMouseClick()
    {
        Action_On_Mouseclick();        
    }
}

In my blazor server side app I want to listen to the mouseclick event on a razor page, and when mouseclick occurs (except on a button), I want to run a method. I have tried following with JavaScript but didn't worked for me:

in my _host.cshtml added following script:

window.addDocumentClickListener = () => {
  document.addEventListener('click', function (e) {
    if (e.target.tagName.toLowerCase() !== 'button') {
      DotNet.invokeMethodAsync('MyProjectName', 'HandleMouseClick');
    }
  });
};

in my Razor page:

@inject IJSRuntime JS
    ...
    ...
@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender )
        {
            await JS.InvokeVoidAsync("addDocumentClickListener");
        }

    }
    [JSInvokable]
    public void HandleMouseClick()
    {
        Action_On_Mouseclick();        
    }
}
Share Improve this question edited Nov 25, 2024 at 9:29 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Nov 16, 2024 at 8:57 MdarendeMdarende 7951 gold badge12 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Here's a demo with no extra JS, using the Counter page. It has a full page div with click event handler. Note that you need to using stopPropagation="true" to prevent bubbling up on buttons and other UI elements with UI events.

@page "/counter"

<PageTitle>Counter</PageTitle>
<div class="underlay" @onclick="BackClick">
    <div>
        <h1>Counter</h1>

        <p role="status">Current count: @currentCount</p>

        <button class="btn btn-primary" @onclick:stopPropagation="true" @onclick="IncrementCount">Click me</button>
    </div>
</div>

<style>
    .underlay {
        position: fixed;
        width: 100%;
        height: 100%;
        background-color: transparent;
    }
</style>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        Console.WriteLine("Button Clicked");
        currentCount++;
    }

    private void BackClick()
    {
        Console.WriteLine("Background Clicked");
    }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论