jQuery 插件及其依赖项的使用指南

发布于 2024-07-30 20:21:51 字数 1432 浏览 3 评论 0原文

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

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

发布评论

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

评论(1

放飞的风筝 2024-08-06 20:21:58

就我个人而言,当我构建插件时,我会尝试使其尽可能少地依赖其他资源,以避免出现此问题。 有时你无法避免使用外部样式表,在这种情况下,我总是将它们与其他 CSS 文件放在一起,即使它们最初包含在与插件相同的文件夹中。 这可以避免在尝试决定哪些样式表应该放在哪里等时出现任何歧义。无论如何,您都必须手动包含它们。 如果插件有任何依赖项,它们会进入 JS 文件夹,其组织方式与其他插件类似。 在这种情况下,图像将与所有其他图像一起使用。

构建插件时,您可以通过允许用户定义应用于某些对象的类,或者让用户定义插件将操作的结构来使其更加灵活。 所有这些都可以在为其提供一组良好的默认值以遵循的同时尽可能少地依赖外部资源的情况下完成。

至于是否针对这些情况定义了最佳实践,我还没有找到。 我只在 jQuery 网站上找到了插件创作指南: http://docs.jquery.com/Plugins/创作

编辑:

澄清插件依赖关系组织:

当你有 jquery.x.js 和 jquery.y.js 时。 它们都依赖于 jquery.z.js。 我总是将 jquery.z.js 与 jquery.x.js 和 jquery.y.js 放在同一文件夹中。 这可以避免重复以及与违反组织惯例相关的任何混乱。 所以:

  • ./jquery.x.js
  • ./jquery.y.js
  • ./jquery.z.js

我通常这样组织我的文件夹:

  • ./js/jquery-xxxjs
  • ./js/plugins/jquery.x.js
  • 。 /js/plugins/jquery.y.js
  • ./js/plugins/jquery.z.js

Personally, when I build a plugin, I try to make it dependent as little as possible on other resources as to avoid this issue. Sometimes you can't avoid using external stylesheets in which case I've always put them with my other CSS files even if they are originally included in the same folder as the plugins. This avoids any ambiguity when trying to decide which stylesheets should go where etc. You will manually have to include them anyways. If the plugin has any dependencies, they go in the JS folder, organized in a similar manner as the other plugins. Images, in this case, would then go with all of the other images.

When building a plugin, you can make it more flexible by allowing the user to define the classes that are applied to certain objects, or let the user define the structure of what the plugin will manipulate. All of this can be done while giving it a set of good defaults to follow while relying as little as possible on external resources.

As to whether best practices have been defined for these situations, I have not found any yet. I have only found the plugin authoring guidelines on the jQuery site: http://docs.jquery.com/Plugins/Authoring.

EDIT:

As to clarify about plugin dependency organization:

When say you have jquery.x.js and jquery.y.js. They both depend on jquery.z.js. I have always put jquery.z.js in the same folder as jquery.x.js and jquery.y.js. This avoids duplication and any confusion related to breaking the organizational convention. So:

  • ./jquery.x.js
  • ./jquery.y.js
  • ./jquery.z.js

I normally organize my folders as such:

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