我应该何时使用JavaScript框架库? [英] When should I use a javascript framework library?

查看:104
本文介绍了我应该何时使用JavaScript框架库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型网站,它只是制作一些动画,并将一些信息显示为主页和链接列表。所有这些都将在客户端动态生成。因此,一切都将成为JavaScript和XML。



最近,我一直在阅读关于JavaScript的一些问题,大多数情况都涉及到使用和/或推荐一个框架(jQuery和朋友)。当一个小型网站开发应该开始考虑使用这样的框架?



我一直在使用纯JavaScript处理我的东西,就我而言没有实现一个大的网站是值得学习的框架?



谢谢 解决方案

在SO上,您会发现很多(尤其是)主张使用jQuery的人(包括我)。对我来说,框架应该是一切:小型,轻量级,可扩展,紧凑但功能强大且简洁的语法,它解决了一些非常重要的问题。我真的很难试图设想一个项目,我不会使用它(或另一个框架)。



使用它的原因是为了解决浏览器兼容性问题的问题。考虑我对 javascript的答案以获得段落/ b>


 函数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 {
返回parent.innerText || parent.textContent;


$ / code $ / pre
$ / $ $
$ b

重新熟悉Javascript,你应该很熟悉这些东西:诸如检查innerText或textContent(Firefox 1.5)等等。纯Javascript是散布着这样的东西。现在考虑jQuery解决方案:

  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? :para.text();
}

jQuery真正发光的地方在于AJAX。有JavaScript代码片断可以找到正确的对象来实例化(XMLHttpRequest或等效)来执行AJAX请求。 jQuery为您处理所有这些事情。



所有这些都在20k以下的核心jQuery Javascript文件中。对我来说,这是必须的。


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框架库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆