【问题标题】:Use Javascript addEventListener within Blazor component在 Blazor 组件中使用 Javascript addEventListener
【发布时间】:2021-06-10 12:30:29
【问题描述】:

我有一个在服务器端呈现的 Blazor 组件。我想在里面放一些可折叠的 div。但是,由于代码是服务器渲染的,因此不会执行 Javascript,因此这些部分不会崩溃。

这是我的script.js 文件中的代码:

var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
    coll[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var content = this.nextElementSibling;
        if (content.style.maxHeight){
            content.style.maxHeight = null;
        } else if(window.matchMedia("(max-width:1440px)")){
            // content.style.maxHeight = content.scrollHeight + "px";
            content.style.maxHeight = "20vh";
        } 
        else {
            content.style.maxHeight = "50vh";
        }
    });
}

这是我的main.cshtml 文件:

<component type="typeof(Main)" render-mode="Server" />

<script src="~/js/script.js" type="text/javascript"></script>

最后是我的Main 带有可折叠部件的组件:

@using Microsoft.AspNetCore.Components;
@using Microsoft.AspNetCore.Components.Web;

<div class="collapsible">
    <label for="tutu">HEADER</label>
    <div id="mybtn" class="btn-rch"></div>
</div>

<div class="tutu content flex-column">
    <p>CONTENT HIDDEN IN COLLAPSE</p>
</div>

<div class="collapsible">
    <label for="tutu">HEADER</label>
    <div id="mybtn" class="btn-rch"></div>
</div>

<div class="tutu content flex-column">
    <p>CONTENT HIDDEN IN COLLAPSE</p>
</div>

<div class="collapsible">
    <label for="tutu">HEADER</label>
    <div id="mybtn" class="btn-rch"></div>
</div>

<div class="tutu content flex-column">
    <p>CONTENT HIDDEN IN COLLAPSE</p>
</div>

@code {

}

如果我使用render-mode="Static" 而不是render-mode="Server" 它可以工作,但是由于我的组件内部有事件,所以对我来说不可能。例如,如何使用 JSInterop 调用我的 JS 脚本来使我的 div 折叠?

【问题讨论】:

    标签: c# blazor blazor-server-side blazor-jsinterop


    【解决方案1】:

    您可以在 Blazor 中完成所有这些操作。下面是一个简单的工作示例,说明我认为您正在努力实现的目标。

    这是一个可折叠的 div 组件。

    CollapseDiv.razor

    <div @onclick="Collapse" style="cursor:pointer;" >
        <h2>@Label</h2>
    </div>
    @if (!Collapsed)
    {
        <div>@ChildContent</div>
    }
    
    @code {
    
        [Parameter] public RenderFragment ChildContent { get; set; }
    
        [Parameter] public RenderFragment Label { get; set; }
    
        bool Collapsed;
    
        void Collapse(MouseEventArgs e)
        {
            Collapsed = !Collapsed;
        }
    }
    

    这是演示它的页面:

    Collapse.razor

    @page "/collapse"
    <h3>Collapse Test Page</h3>
    
    <CollapseDiv>
        <Label>I'm Collapsible</Label>
        <ChildContent>
            I'm the collapsed content!
        </ChildContent>
    </CollapseDiv>
    <br />
    <br />
    <CollapseDiv>
        <Label>I'm Collapsible Too</Label>
        <ChildContent>
            More collapsed content!
        </ChildContent>
    </CollapseDiv>
    
    @code {
    
    }
    

    这里的关键是:忘记用 Javascript 操作 DOM,构建组件。

    您应该能够采用它来满足您的需求。

    【讨论】:

    • 遗憾的是,我们需要添加一些动画并使用 javascript 更改内容的类别。但我会牢记您的解决方案,因为它非常简单干净!
    猜你喜欢
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2021-11-23
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多