javascript - 通过javascript更改div中属性的事件监听器

标签 javascript jquery html css

我的 div 如下所示:

<div id="modal"
     style="position: fixed;
       left: 0px;
       top: 0px;
       width: 100%;
       height: 100%;
       opacity: 0.4;
       background-color: rgb(204, 204, 204);
       display: block;
       z-index: 99;">
</div>

我希望当我的 div 更改为(注意:显示 block 已更改为无)时发生一些事情:

<div id="modal"
     style="position: fixed;
       left: 0px;
       top: 0px;
       width: 100%;
       height: 100%;
       opacity: 0.4;
       background-color: rgb(204, 204, 204);
       display: none;
       z-index: 99;">
</div>

我在考虑一个 EventListener,有人知道如何让它工作吗?

最佳答案

你可以使用 MutationObserver

有点像

var target = document.getElementById('modal');

var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.attributeName === 'style') {
      ...
    }
  });    
});

关于javascript - 通过javascript更改div中属性的事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41239039/

相关文章:

javascript - 无法在具有模态 : true 的对话框中选择 jquery 多选过滤器

javascript - 在移动设备上切换 bootstrap input-group-addon 和 input 的位置

javascript - jquery this.siblings 无法正常工作

html - 如何使用 CSS 将两种不同透明度的颜色混合在一起

html - 在 CSS 中选中复选框时更改 H1 的颜色

javascript - IMG 周围的边框

javascript - float 在 iframe html 上

JavaScript设置HTTP请求 header value 和FireFox问题

php - 尝试使用 jquery 添加 html 表时 href 显示为 null

html - CSS 全屏 - 绝对位置与 100% 高度