jQuery Tips and Tricks

Syntax Shorthand for the ready-event by roosteronacid Line breaks and chainability by roosteronacid Nesting filters by Nathan Long Cache a collection and execute commands on the same line by roosteronacid Contains selector by roosteronacid Defining properties at element creation by roosteronacid Access jQuery functions as you would an array by roosteronacid The noConflict function

jQuery提示和技巧

句法 由罗斯塔纳西德准备事件的速记 由罗颂酸引起的断行和可链接性 由Nathan Long嵌套过滤器 通过roosteronacid缓存集合并在同一行执行命令 包含由roosteronacid选择 在由roosteronacid创建元素时定义属性 像访问roosteronacid的数组一样访问jQuery函数 noConflict函数 - Oli释放$变量 用nickf在noConflict模式下隔离$变量 Roosteronacid没有冲突模式 数据存储 数据函数 - 通过TenebrousX将数据绑定到

esque OOP in JavaScript and a jQuery fail

I'm working on a project and I'm really trying to write object-oriented JavaScript code. I have just started reading Douglas Crockford's JavaScript: The Good Parts and I'm quickly beginning to realize that writing Java-esque OOP in JavaScript will be a difficult task. Thus far, I've written something like the following... // index.html $(document).ready(function() { $(

JavaScript中的OOP和jQuery失败

我正在开发一个项目,并且我正在尝试编写面向对象的JavaScript代码。 我刚开始阅读道格拉斯克罗克福德的JavaScript:The Good Parts,我很快就开始意识到用JavaScript编写Java-esque OOP将是一项艰巨的任务。 到目前为止,我已经写了类似下面的内容...... // index.html $(document).ready(function() { $().SetUpElements(); }); // this is in a different js file $.fn.SetUpElements = function() { // do stuf

How to isolate my javascript code to prevent collisions?

I have been writing a lot of javascript functions and event listeners and I want to move them into their own namespaced, isolated place that doesn't conflict when I concatenate and minify it with my other javascript files. I am still new to javascript, so there may be simple solution to this answer. I started by creating a javascript object: var MySpecialStuff = { init: function(){

如何隔离我的JavaScript代码以防止碰撞?

我一直在写很多JavaScript函数和事件监听器,我想将它们移动到它们自己的命名空间中,当我将其与其他javascript文件连接并缩小时,不会发生冲突。 我仍然对JavaScript不熟悉,所以可能有简单的解决方案来解决这个问题。 我开始创建一个JavaScript对象: var MySpecialStuff = { init: function(){ //do everything here } }; 然后在我的html中,我想要使用它的页面上,我可以初始化这段代码: <script&g

What's the best way to create a JavaScript namespace?

I have not yet found a common way on the Internet for creating a namespace in JavaScript. What's the best way to create a namespace (and please list any downfalls that particular approach might have). (function() { var usefulVariable; var foo = { bar:function(){ alert('hi') } }; // leak into global namespace window.foo = foo; })(); 只有foo暴露于全局名称空间,

什么是创建JavaScript名称空间的最佳方式?

我还没有找到互联网上用JavaScript创建名称空间的常用方法。 创建名称空间的最佳方式是什么(请列出特定方法可能存在的任何问题)。 (function() { var usefulVariable; var foo = { bar:function(){ alert('hi') } }; // leak into global namespace window.foo = foo; })(); 只有foo暴露于全局名称空间,并且它“存在于”执行匿名函数名称空间的私有自执行程序中。 在JavaScript中,命名空间

Best way to define a function?

I'm always learned to define a function in JavaScript like this: function myFunction(arg1, arg2) { ... } However, I was just reading Google's guide to Javascript, it mentioned I should define methods like this: Foo.prototype.bar = function() { ... }; Question : Is "Foo" in the example an Object, or is it a namespace? Why isn't the Google example the following code (whic

定义函数的最佳方法是什么?

我总是学会如何在JavaScript中定义一个函数: function myFunction(arg1, arg2) { ... } 然而,我刚刚阅读了Google的Javascript指南,它提到我应该定义这样的方法: Foo.prototype.bar = function() { ... }; 问题 :在对象的例子中是“Foo”还是命名空间? 为什么不是谷歌示例下面的代码(这是行不通的): prototype.bar = function() { ... }; 更新 :万一它有助于知道,我的所有JavaScript将被用户浏览器调用我的Web应

Why do I encapsulate a jQuery function within jQuery(function ($) { });

I came across a piece of code which looks like this: jQuery(function($) { $('#saySomething').click(function() { alert('something'); }); }); I don't quite get it. Why can't I simply do this: $('#saySomething').click(function() { alert('saySomething'); }); jQuery(function ($) {...}); is the shorthand version of: jQuery(document).ready(function ($) {...}); If you don't

为什么我在jQuery中封装jQuery函数(function($){});

我遇到了一段代码,如下所示: jQuery(function($) { $('#saySomething').click(function() { alert('something'); }); }); 我不太明白。 为什么我不能简单地这样做: $('#saySomething').click(function() { alert('saySomething'); }); jQuery(function ($) {...}); 是以下简写版本: jQuery(document).ready(function ($) {...}); 如果你不等待document准备就绪,那么你将绑定事件的元素将不会存在于dom中

JavaScript Namespace Declaration

I created a javascript class as follow: var MyClass = (function() { function myprivate(param) { console.log(param); } return { MyPublic : function(param) { myprivate(param); } }; })(); MyClass.MyPublic("hello"); The code above is working, but my question is, how if I want to introduce namespace to that class. Basically I want to be able to call the cla

JavaScript命名空间声明

我创建了一个JavaScript类,如下所示: var MyClass = (function() { function myprivate(param) { console.log(param); } return { MyPublic : function(param) { myprivate(param); } }; })(); MyClass.MyPublic("hello"); 上面的代码正在工作,但我的问题是,如果我想为该类引入名称空间。 基本上我想能够像这样调用类: Namespace.MyClass.MyPublic("Hello World"); 如果我

Does this way of defining JS objects have any purpose?

I'm maintaining some legacy code and I've noticed that the following pattern for defining objects is used: var MyObject = {}; (function (root) { root.myFunction = function (foo) { //do something }; })(MyObject); Is there any purpose to this? Is it equivalent to just doing the following? var MyObject = { myFunction : function (foo) { //do something }

这种定义JS对象的方式有什么用处吗?

我维护一些遗留代码,我注意到使用了以下用于定义对象的模式: var MyObject = {}; (function (root) { root.myFunction = function (foo) { //do something }; })(MyObject); 这有什么目的吗? 这相当于只是做了以下事情吗? var MyObject = { myFunction : function (foo) { //do something }; }; 我不打算进行一个神圣的探索来重构整个代码库以符合我的喜好,但我真的很想了解这种

page JavaScript web application?

How should a complex single-page JS web application be structured on the client-side? Specifically I'm curious about how to cleanly structure the application in terms of its model objects, UI components, any controllers, and objects handling server persistence. MVC seemed like a fit at first. But with UI components nested at various depths (each with their own way of acting on/reacting to

页面JavaScript Web应用程序?

应该如何在客户端构建复杂的单页JS Web应用程序? 具体来说,我很好奇如何根据模型对象,UI组件,任何控制器和处理服务器持久性的对象干净地构建应用程序。 MVC起初似乎很合适。 但是,UI组件嵌套在各种深度(每种都有自己的方式来处理/响应模型数据,并且每种方式都会生成它们自己可能会或可能不会直接处理的事件),但似乎并不像MVC那样可以清晰地应用。 (但是,如果情况并非如此,请纠正我。) - (这个问题导致

I have declared a global variable in script1,js

This question already has an answer here: How do I declare a namespace in JavaScript? 26 answers You should capsule your code in each file, so you don´t pollute your windows object. Also, you can use "return" to get values from the other file, if needed.

我已经在script1中声明了一个全局变量js

这个问题在这里已经有了答案: 我如何在JavaScript中声明一个名称空间? 26个答案 你应该将你的代码封装在每个文件中,这样你就不会污染你的windows对象。 另外,如果需要,您可以使用“返回”从另一个文件中获取值。