简体   繁体   English

使用 JavaScript 拦截从 DOM 中移除的元素上的点击事件

[英]Intercept click event on the element that is removed from the DOM with JavaScript

I have a little weird situation.我有一个有点奇怪的情况。 I need to perform a click event (intercept), here is my case.我需要执行一个点击事件(拦截),这是我的情况。 By default element (div) are removed from the DOM, that means that div only appears when the user clicks the button, below is an image of that button默认情况下,元素 (div) 从 DOM 中删除,这意味着 div 仅在用户单击按钮时出现,下面是该按钮的图像

在此处输入图片说明

When the user clicks the button, content appears, image below:当用户点击按钮时,出现内容,如下图:

在此处输入图片说明

What am trying to achieve?想要达到什么目标?

Am trying to achieve this, when users click the button I want to add style to one div that appears, I simulate that on the image below:我正在尝试实现这一点,当用户单击我想向出现的一个 div 添加样式的按钮时,我在下图中进行了模拟:

在此处输入图片说明

So far my code looks like this:到目前为止,我的代码如下所示:

var buttons = document.querySelectorAll('button.h5p-image-hotspot');
  for (var i = 0; i < buttons.length; i++) {
      buttons[i].addEventListener('click', function(){
        var myNodelist = document.querySelectorAll('.h5p-image')
        var i;
        for (i = 0; i < myNodelist.length; i++) {
          myNodelist[i].style.width = "300px";
        }
      });
  }

But this doesn't work, can anybody try to help me with this?但这不起作用,有人可以帮我解决这个问题吗?

@cloned already gave a good hint IMHO. @cloned 已经给出了一个很好的提示恕我直言。 Overriding the stylesheet would be the cleanest way to achieve this, since you want all images to be affected anyway.覆盖样式表将是实现此目的的最简洁方法,因为无论如何您都希望所有图像都受到影响。

With H5P, you have multiple options now that all have their pros and cons.使用 H5P,您现在有多种选择,各有优缺点。

Patching打补丁

You can patch the code for instance.例如,您可以修补代码。 Then all you'd have to do is to add那么你所要做的就是添加

.h5p-image-hotspot-popup-body-fraction.h5p-image {
    width: 300px;
}

at https://github.com/h5p/h5p-image-hotspots/blob/master/styles/image-hotspots.css#L303 on your system.在您的系统上的https://github.com/h5p/h5p-image-hotspots/blob/master/styles/image-hotspots.css#L303 However, whenever you update the H5P Image Hotspot content type, those changed would have to be re-applied since the official code would override them.但是,无论何时更新 H5P Image Hotspot 内容类型,更改后的内容都必须重新应用,因为官方代码会覆盖它们。

Forking分叉

You could do the same in a fork, meaning you'd have to clone the complete content library files and change the machineName in library.json ( https://github.com/h5p/h5p-image-hotspots/blob/master/library.json#L10 ) and the occurrences in the code itself (it's the class name as well).您可以在 fork 中执行相同操作,这意味着您必须克隆完整的内容库文件并更改 library.json 中的 machineName ( https://github.com/h5p/h5p-image-hotspots/blob/master/ library.json#L10 )和代码本身中的出现(它也是类名)。 The downside here is that you'd have to merge updates of the original codes to your fork if you want to benefit from bugfixes or new features.这里的缺点是,如果您想从错误修正或新功能中受益,您必须将原始代码的更新合并到您的分支中。

Hooking挂钩

Luckily, H5P offers hooks to add scripts or to alter the stylesheets which is described at https://h5p.org/documentation/for-developers/visual-changes You will have to choose the proper plugin for your host system (eg WordPress, Drupal or moodle - the native core version of moodle 3.8+ doesn't feature hooks unfortunately, only the H5P plugin does), implement the alter_styles function to load your CSS file (see examples at https://h5p.org/documentation/for-developers/visual-changes ) and then put the aforementioned CSS to that file.幸运的是,H5P 提供了用于添加脚本或更改样式表的钩子,在https://h5p.org/documentation/for-developers/visual-changes 中描述您必须为您的主机系统选择合适的插件(例如 WordPress、 Drupal 或moodle - 不幸的是,moodle 3.8+ 的原生核心版本没有钩子,只有H5P 插件有),实现alter_styles 函数来加载你的CSS 文件(参见https://h5p.org/documentation/for 上的示例) -developers/visual-changes ),然后将上述 CSS 放入该文件。 Now, whenever H5P loads content, it will use your stylesheet to override its own without the need to touch the original H5P code, so you can benefit from updates while not having to re-apply patches.现在,每当 H5P 加载内容时,它都会使用您的样式表来覆盖自己的样式表,而无需触及原始 H5P 代码,因此您可以从更新中受益,而无需重新应用补丁。

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

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