我什么时候应该使用 javascript 框架库?

2023-06-14前端开发问题
4

本文介绍了我什么时候应该使用 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.

这篇关于我什么时候应该使用 javascript 框架库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13