如何限制元素及其子元素的 DOM 交互?

发布于 2024-08-11 20:44:14 字数 574 浏览 4 评论 0原文

我正在为 PHP MVC 站点构建模块化框架。我正在使用 jQuery。我有一个 registerModule('module_name') 方法,调用该方法时会创建具有此名称的模块对象的实例。这些模块对象是返回包含在各个脚本文件中的对象的函数。

test_module.js 示例:

core.modoules.test_module = function(sandbox){
  return{
    init : function(){
      
    }
  };
};

registerModule() 方法中,我正在处理继承和初始化。在初始化时,我想检查 DOM 中是否有与此模块匹配的元素。对于此示例,我们将查找 id 为 test_modulediv,并且我希望将 test_module 对象内的任何 DOM 交互限制为在该 DOM 范围内。我目前正在使用 jQuery 的选择器,但感觉我可能需要编写自己的 DOM 包装器或其他东西。对此有什么想法吗?

I am building a modular framework for a PHP MVC site. I am using jQuery. I have a registerModule('module_name') method that when called creates an instance of a module object with this name. These module objects are functions that return an object they are contained within individual script files.

Example of test_module.js:

core.modoules.test_module = function(sandbox){
  return{
    init : function(){
      
    }
  };
};

Within the registerModule() method I am handling inheritance and initialization. On initialization, I would like to check the DOM for an element matching this module. For this example, we'd look for a div with id test_module and I would like to limit any DOM interaction within the test_module object to be within that DOM scope. I currently am using jQuery's selectors but have the feeling I may need to write my own DOM wrapper or something. Any ideas on this?

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

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

发布评论

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

评论(1

何处潇湘 2024-08-18 20:44:14

$() 采用第二个参数,它限制了搜索范围

$(selector, context)

,这实际上与

$(context).find(selector);

$() takes a 2nd parameter which limits the search scope

$(selector, context)

which is really the same as

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