当 div 从可见状态变为隐藏时调用 Javascript 函数

发布于 2024-08-05 02:08:08 字数 187 浏览 4 评论 0原文

我想在 div 从“visibilty:hidden”变为“visibility:none;”时调用 Javascript 函数

另请注意,我无法控制转换 div 的此样式属性的脚本。我只是想参与其中。有什么可能性吗?或者像 onFocus() 等?

更新:我不想使用 JQuery 或其他框架。是否可以?

I want to call a Javascript function when a div is turned from "visibilty : hidden" to "visibility : none;"

Also note that I don't have control over the script which turns this style property of the div. I just want to hook into this. Any possibilities? Or like onFocus() etc?

UPDATE : I do not want to use JQuery or other frameworks. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小耗子 2024-08-12 02:08:08

在 mootools 中,您可以创建自定义事件。但是,我会这样做:

document.getElementById('foo').triggerMyEvent = function() {
  if (this.style.visibility == 'hidden') {
    // do something
  } else {
    // do something else
  }
}

并在切换对象可见性的任何代码中添加对对象的“triggerMyEvent”方法的调用。

In mootools you can create custom events. However, I would do something like this:

document.getElementById('foo').triggerMyEvent = function() {
  if (this.style.visibility == 'hidden') {
    // do something
  } else {
    // do something else
  }
}

And add a call to the object's 'triggerMyEvent' method in whatever code switches the object's visibility.

宛菡 2024-08-12 02:08:08

IE 中有一个 propertychange 事件,它响应元素属性的更改,包括其 style 对象的属性。但是,这仅适用于直接在元素的 style 对象上设置的属性,不适用于间接影响元素样式的 CSS 更改(例如更改元素的父元素的类)。在其他浏览器中使用 DOMAttrModified 的工作方式类似,并且具有相同的缺点,因此这可能不适合您。

There's the propertychange event in IE that responds to changes in an element's properties, including properties of its style object. However, this only works on properties set directly on the element's style object and doesn't work for CSS changes (e.g. changing the class of the element's parent element) that indirectly affect the element's style. Using the DOMAttrModified in other browsers will work similarly and has the same shortcomings, so this may not be workable for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文