我什么时候应该使用 javascript 框架库?
我正在编写一个小型网站,它只是制作一些动画并将一些信息显示为主页和链接列表.所有这些都将在客户端动态生成.所以一切都将是 javascript 和 XML.
最近我一直在阅读 SO 中有关 javascript 的一些问题,并且大多数情况涉及框架的使用和/或推荐(jquery 和朋友).小型 Web 开发何时应该开始考虑使用这样的框架?
到目前为止,我一直只使用普通的 javascript 来做我的事情,就我没有实现大型网站而言,是否值得学习一个框架?
谢谢
解决方案在 SO 上你会发现 很多 人(包括我)提倡使用 jQuery(尤其是).对我来说,这是一个框架应该具备的一切:小型、轻量级、可扩展、紧凑但功能强大且语法简洁,它解决了一些非常重要的问题.老实说,我很难想象一个我不会使用它(或其他框架)的项目.
使用它的原因是为了解决浏览器的兼容性问题.考虑我对 javascript 获取段落的回答网页中所选文本的数量:
<块引用>函数 getSelectedParagraphText() {var 用户选择;如果(window.getSelection){选择 = window.getSelection();} else if (document.selection) {选择 = document.selection.createRange();}var parent = selection.anchorNode;while (parent != null && parent.localName != "P") {父 = 父节点;}如果(父母==空){返回 "";} 别的 {返回 parent.innerText ||父.文本内容;}}
如果您熟悉 Javascript,那么您应该熟悉其中的很多内容:检查 innerText 或 textContent (Firefox 1.5) 等等.纯 Javascript 充斥着这样的东西.现在考虑 jQuery 解决方案:
函数 getSelectedParagraphText() {var 用户选择;如果(window.getSelection){选择 = window.getSelection();} else if (document.selection) {选择 = document.selection.createRange();}var parent = selection.anchorNode;var paras = $(parent).parents("p")返回 paras.length == 0 ?"" : paras.text();}
jQuery 真正闪耀的地方在于 AJAX.周围有 JavaScript 代码片段来找到正确的对象来实例化(XMLHttpRequest 或等效的)来执行 AJAX 请求.jQuery 会为您处理所有这些.
对于核心 jQuery Javascript 文件,所有这些都在 20k 以下.对我来说,这是必须的.
I'm writing a small web that just makes some animation and shows some information as a homepage and a list of links. All that is going to be generated dynamically in the client side. So everything is going to be javascript and XML.
Recently I've been reading some questions in SO about javascript, and most of the situations involved the use and/or recommendation of a framework (jquery and friends). When a small web development should start considering the use of such a framework?
I've been until now doing my stuff just with plain javascript, as far as I'm not implementing a big site is it worth the learning a framework?
Thanks
解决方案On SO you will find a lot of people (including me) who advocate the use of jQuery (in particular). To me, it's everything a framework should be: small, lightweight, extensible, compact yet powerful and brief syntax and it solves some pretty major problems. I would honestly have a hard time trying to envision a project where I wouldn't use it (or another framework).
The reason to use it is to solve browser compatibility issues. Consider my answer to javascript to get paragraph of selected text in web page:
function getSelectedParagraphText() { var userSelection; if (window.getSelection) { selection = window.getSelection(); } else if (document.selection) { selection = document.selection.createRange(); } var parent = selection.anchorNode; while (parent != null && parent.localName != "P") { parent = parent.parentNode; } if (parent == null) { return ""; } else { return parent.innerText || parent.textContent; } }
If you're familiar with Javascript a lot of this should be familiar to you: things like the check for innerText or textContent (Firefox 1.5) and so on. Pure Javascript is littered with things like this. Now consider the jQuery solution:
function getSelectedParagraphText() {
var userSelection;
if (window.getSelection) {
selection = window.getSelection();
} else if (document.selection) {
selection = document.selection.createRange();
}
var parent = selection.anchorNode;
var paras = $(parent).parents("p")
return paras.length == 0 ? "" : paras.text();
}
Where jQuery really shines though is with AJAX. There JavaScript code snippets around to find the correct object to instantiate (XMLHttpRequest or equivalent) to do an AJAX request. jQuery takes care of all that for you.
All of this for under 20k for the core jQuery Javascript file. To me, it's a must-have.
相关文章