我有一个内容可编辑的块,它有一个工具箱.当您将焦点放在内容可编辑块上时,工具箱将出现(它是实际工具栏的折叠按钮).如果您打开工具箱,需要将Bootstrap类"active"添加到工具箱中.如果我们完全将焦点放在内容块之外,该工具箱应该会消失(一个新的工具箱会出现在不同的内容块上).

由于需要单击工具箱,我无法让第三个事件侦听器按预期工作.

超文本标记语言:

.border.border-dark.p-4
        .position-relative.w-100{data: {controller: "content", content:{block:{value: true}}}}
            .position-absolute.bg-white{style: "right: 101%"}
                %button.d-none.btn.btn-outline-dark{type: "button", data: {content_target: "toolbox", bs:{toggle: "collapse", target: "#toolbar1"}}}
                    %i.bi.bi-hammer
            
            #toolbar1.collapse.position-absolute.bottom-100.bg-white.btn-toolbar{role: "toolbar", aria:{label: "Toolbar with button groups"}}
                .btn-group.me-2{role: "group", aria:{label: "First group"}}
                    %button.btn.btn-outline-dark.active{type: "button"} H2
                    %button.btn.btn-outline-dark{type: "button"} H3
                    %button.btn.btn-outline-dark{type: "button"} H4
                    %button.btn.btn-outline-dark{type: "button"} H5
                    %button.btn.btn-outline-dark{type: "button"} H6
                    %button.btn.btn-outline-dark{type: "button"}
                        %i.bi.bi-paragraph
                    
            %p{contenteditable: true, data: {content_target: "contentBlock"}} This is some text

以及刺激计划

this.contentBlockTarget.addEventListener("focus", (event) => {
    this.toolboxTarget.classList.remove("d-none")
})
this.toolboxTarget.addEventListener("click", (event) => {
    this.toolboxTarget.classList.add("active")
    this.toolboxTarget.classList.remove("active")
})  
this.contentBlockTarget.addEventListener("focusout", (event) => {
    this.toolboxTarget.classList.add("d-none")
})

以下是我的要求:

  1. If user focuses on the contentBlock (in this HTML it's a

    ),应出现工具箱(删除类"d-None").(First Event Listener在这方面做得很好)

  2. 如果用户点击工具箱,则添加"活动"类.(如果将第三个事件侦听器注释掉,则可以很好地工作).如果第三个侦听器处于活动状态,则单击工具箱会使其消失,因此工具栏不会实际出现.
  3. 如果用户在工具箱之外的任何地方单击,或者光标不再位于该区域(如将光标保持在该区域中),则需要重新添加"d-None".

推荐答案

解决此问题的一种方法是结合使用事件侦听器和CSS类.

首先,默认情况下可以将d-none类添加到工具箱中.然后,您可以在内容可编辑块上使用focus事件侦听器来移除d-none类并使工具箱出现.

若要在单击工具箱时将active类添加到工具箱中,可以在工具箱本身上使用click事件侦听器.在事件侦听器中,您可以将active类添加到工具箱,然后使用setTimeout函数在短暂延迟后将其删除.

最后,要在工具箱不在焦点时隐藏它,可以在内容可编辑块上使用focusout事件侦听器.在事件侦听器中,您可以使用active类判断用户是否已经单击了工具箱.如果他们没有点击工具箱,您可以添加d-none类来隐藏它.

// Add the "d-none" class to the toolbox by default
this.toolboxTarget.classList.add("d-none");

// Show the toolbox when the content editable block is focused
this.contentBlockTarget.addEventListener("focus", (event) => {
    this.toolboxTarget.classList.remove("d-none");
});

// Add the "active" class to the toolbox when it is clicked
this.toolboxTarget.addEventListener("click", (event) => {
    this.toolboxTarget.classList.add("active");
    setTimeout(() => {
        this.toolboxTarget.classList.remove("active");
    }, 100); // Remove the "active" class after a brief delay
});

// Hide the toolbox when it is not in focus
this.contentBlockTarget.addEventListener("focusout", (event) => {
    if (!this.toolboxTarget.classList.contains("active")) {
        this.toolboxTarget.classList.add("d-none");
    }
});

Javascript相关问答推荐

django无法解析余数:[0] from carray[0]'

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

从Node JS将对象数组中的数据插入Postgres表

如何在JavaScript文件中使用Json文件

如何将Cookie从服务器发送到用户浏览器

加载背景图像时同步旋转不显示的问题

Chart.js-显示值应该在其中的引用区域

不能将空字符串传递给cy.containes()

在forEach循环中获取目标而不是父对象的属性

将对象推送到数组会导致复制

如何在HTMX提示符中设置默认值?

打字脚本中方括号符号属性访问和拾取实用程序的区别

WebSocketException:远程方在未完成关闭握手的情况下关闭了WebSocket连接.&#三十九岁;

处理app.param()中的多个参数

Reaction useState和useLoaderData的组合使用引发无限循环错误

将Singleton实例设置为未定义后的Angular 变量引用持久性行为

在传单的图像覆盖中重新着色特定 colored颜色 的所有像素

我如何才能让p5.js在不使用实例模式的情况下工作?

使用Java脚本筛选数组中最接近值最小的所有项

如何导入我在Web Worker创建的函数?