我试图保持我的 html 干净,因此通过我的 app.js 文件需要库/插件,所有 javascript/jquery 逻辑都发生在该文件中。我正在使用 FastClick 库来省略 300 毫秒的点击延迟并以这种方式对其进行初始化。
jQuery(document).ready(function ($) {
'use strict';
/* Load and initialize FastClick, ommits 300ms tap delay */
$.getScript('lib/fastclick.js', function () {
new FastClick(document.body);
}); /* END FastClick */
}); /* END jQuery */
这是否具有语义意义,使用它而不是在 html 页面中添加 fastclick.js 文件会更有害吗?
最佳答案
它具有语义意义,并且从 fastclick docs 判断, 它看起来没有害处。
Include fastclick.js in your JavaScript bundle or add it to your HTML page...
他们说将其作为串联文件或独立文件包含在内是安全的。您可以随意包含 FastClick 源代码,但您认为合适。
他们的文档确实使 FastClick 的初始化变得复杂。 document.documentElement
属性在 JavaScript 开始执行时可用,代表 <html>
元素。这可以用作 FastClick 的根元素,从而不需要 DOMContentLoaded 或 domready 事件处理程序:
new FastClick(document.documentElement);
不需要事件处理程序。现在,您可以根据需要在页面加载时响应点击,并且在完整的 HTML 文档被解析之前。
编辑:@jungy 在他的评论中提出了一个很好的观点:
... you are waiting till the DOM is ready before downloading the library.
完整代码示例:
jQuery.getScript("lib/fastclick.js", function() {
new FastClick(document.documentElement);
});
关于javascript - 这是初始化快速点击的好方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28433123/