問題描述
我正在編寫一個小型網站,它只是制作一些動畫并將一些信息顯示為主頁和鏈接列表.所有這些都將在客戶端動態生成.所以一切都將是 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.
這篇關于我什么時候應該使用 javascript 框架庫?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!