Javascript Onready函数调用Namespace函数

发布于 2024-10-02 20:32:46 字数 1035 浏览 0 评论 0原文

我正在删除 Rails/views/show.html.erb 中使用的 Javascript 中的全局变量

我使用的 javascript 是

var App = {};
App.UserSnapShot = function () {
  var _body, _detailEl, _selectedLinkEl;

  var _init = function () {
    _body = $$("body")[0];

    $$(".user-link").each(function (el) {
      el.observe("mouseover", function (event) {
        _selectedLinkEl = el;
        _detailEl = event.element().next();
        _detailEl.show();
        _body.observe("mouseover", _bodyMouseOverFunction);
      });
    });
  };

  var _bodyMouseOverFunction = function (event) {
    if (event.element() != _selectedLinkEl && 
        event.element() != _detailEl && 
        !event.element().ancestors().include(_detailEl)) {
      _detailEl.hide();
      _body.stopObserving("mouseover", _bodyMouseOverFunction);
    }
  };

  return {
    init: function () {
      _init();
    }
  };
}();

我需要调用我的函数 App.UserSnapShot.init();通过相同的 onready 函数..,这样才能让它发挥作用.. 我如何在这里添加 onReady 函数。

请给一些建议

i am removing the global variables in the Javascript that is used in my Rails/views/show.html.erb

The javascript that i am using is

var App = {};
App.UserSnapShot = function () {
  var _body, _detailEl, _selectedLinkEl;

  var _init = function () {
    _body = $("body")[0];

    $(".user-link").each(function (el) {
      el.observe("mouseover", function (event) {
        _selectedLinkEl = el;
        _detailEl = event.element().next();
        _detailEl.show();
        _body.observe("mouseover", _bodyMouseOverFunction);
      });
    });
  };

  var _bodyMouseOverFunction = function (event) {
    if (event.element() != _selectedLinkEl && 
        event.element() != _detailEl && 
        !event.element().ancestors().include(_detailEl)) {
      _detailEl.hide();
      _body.stopObserving("mouseover", _bodyMouseOverFunction);
    }
  };

  return {
    init: function () {
      _init();
    }
  };
}();

I need to call my function App.UserSnapShot.init();through onready function of the same .. , So that to get this to work ..
How can i add a onReady function here.

Pls give some suggestions

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

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

发布评论

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

评论(2

寄离 2024-10-09 20:32:46

你的意思是?

App.onReady = function() {
    App.UserSnapShot.init();
};

document.observe("dom:loaded", App.onReady);

You mean?

App.onReady = function() {
    App.UserSnapShot.init();
};

document.observe("dom:loaded", App.onReady);
○愚か者の日 2024-10-09 20:32:46

使用 jQuery...

$(document).ready(function(){
// 在此处插入在文档加载时运行的代码
});

Using jQuery...

$(document).ready(function(){
// Insert code to run on document load here
});

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