简体   繁体   English

在 Blazor 组件中使用 Javascript addEventListener

[英]Use Javascript addEventListener within Blazor component

I have a Blazor component which is rendered server-side.我有一个在服务器端呈现的 Blazor 组件。 And I would like to have some collapsible divs inside of it.我想在里面放一些可折叠的 div。 However since the code is server rendered the Javascript is not executed therefore the parts cannot collapse.然而,由于代码是服务器渲染的,Javascript 没有被执行,因此部件不能折叠。

Here is the code inside my script.js file:这是我的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";
        }
    });
}

Here is my main.cshtml file:这是我的main.cshtml文件:

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

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

And finally my Main component with the collapsible parts:最后是带有可折叠部分的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 {

}

If I use render-mode="Static" instead of render-mode="Server" it works, but since my component will have event inside of it is not a possibility for me.如果我使用render-mode="Static"而不是render-mode="Server"它可以工作,但是因为我的组件内部会有事件,所以对我来说是不可能的。 How can I, with the use of JSInterop for example, call my JS script to make my div collapse?例如,我如何使用 JSInterop 调用我的 JS 脚本来使我的 div 折叠?

You can do all this in Blazor.您可以在 Blazor 中完成所有这些操作。 Below is a simplistic working example of what I think you are trying to achieve.以下是我认为您正在努力实现的一个简单的工作示例。

This is a collapsible div component.这是一个可折叠的 div 组件。

CollapseDiv.razor 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;
    }
}

And this is the page to demo it:这是演示它的页面:

Collapse.razor折叠.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 {

}

The key here is: Forget manipulating the DOM with Javascript, build components.这里的关键是:忘记使用 Javascript 操作 DOM,构建组件。

You should be able to adopt this to fit your needs.您应该能够采用它来满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM