Javascript document.observe('dom:loaded', function() {

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1907702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:50:33  来源:igfitidea点击:

document.observe('dom:loaded', function() {

javascriptruby-on-railsprototypejs

提问by thenengah

Is there a way to have this prototype js trigger only when dom is changed and not loaded?

有没有办法让这个原型js只在dom改变而不加载时触发?

回答by robjmills

you can observe elements changing like this

你可以观察到元素像这样变化

$('element').observe('change',function(e){ } );

This is reserved for form elements though - textarea, select and input.

这是为表单元素保留的 - 文本区域、选择和输入。

The final code would look something like:

最终代码如下所示:

document.observe('dom:loaded', function() {
    $('element').observe('change',function(e){
    // do something here
    });
});

回答by Colin Fine

The 'change' method is defined only for 'input', 'textarea' and select elements, not for general elements.

'change' 方法只为 'input'、'textarea' 和 select 元素定义,而不是为一般元素定义。

The "dom:loaded" event is a user-defined event (as far as the browser is concerned) defined by the Prototype library. I don't believe that it is usable as any kind of template for a dom:changed event.

"dom:loaded" 事件是由 Prototype 库定义的用户定义事件(就浏览器而言)。我不相信它可以用作 dom:changed 事件的任何类型的模板。

What you are looking for are DOM mutation events, such as DomSubtreeModified (see 1). But I don't believe these are widely supported in browsers yet.

您正在寻找的是 DOM 突变事件,例如 DomSubtreeModified(参见1)。但我不相信这些在浏览器中得到广泛支持。