类型错误:document.observe 不是函数

Siv*_* KB 1 javascript jquery ruby-on-rails ruby-on-rails-3

在我的 js 文件中我是这样写的

 document.observe("dom:loaded", function() {
        add();
    });
Run Code Online (Sandbox Code Playgroud)

但它显示错误TypeError: document.observe is not a function

我正在使用 rails 3 & jquery-ui-1.10.0

谁能告诉我为什么我会这样?

提前致谢

Dip*_*pta 5

由于 rails 3 使用 jquery,而您正在原型中编写代码。这可能会在这里产生问题。

所以尝试在 jquery 中转换你的代码。您的代码可能看起来像。

$( document ).ready(function() {
// Run code
});
Run Code Online (Sandbox Code Playgroud)

但请考虑以下并根据您的需要进行更改

$(window).load(function(){
  // Run code ##It will run the js after the whole page is loaded.
});

$(document).ready(function(){
 // Run code ##It will run the js just after the loading of DOM. 
}); 
Run Code Online (Sandbox Code Playgroud)