Tag: 原型

输入具有指定值时隐藏div

当值= 10时,我试图隐藏div 这是代码和演示正常工作: $(‘input[name=test]’).keyup(function(){ if($(this).val()<10) $('#yeah').show(); else $('#yeah').hide(); }); Type whatever 但我正在尝试将该代码转换为Prototype代码,我尝试了这段代码 : Event.observe(window, ‘load’, function () { $(‘input[name=test]’).keyup(function(){ if($(this).val()<10) $('#yeah').show(); else $('#yeah').hide(); }); }); 我只想在输入值= 10时将div隐藏到原型代码中。 请有人可以帮帮我吗?

在Prototye中定义处理程序函数

我正在尝试创建一个JavaScript原型,它允许我以类似于jQuery ajax调用的方式处理进程的结果。 例如: var request = $.ajax({ //params here }) .done(function(data, textStatus, jqXHR) { //action here }) .fail(function (jqXHR, textStatus, errorThrown) { //action here }); 如何声明我的原型有done和fail事件(如果这甚至是正确使用的术语)并确保它们是在原型逻辑中的条件下触发的? 我希望的电话会是这样的: var o = myObj({ parma1: ‘a’, param2: ‘b’ }) .done(function() { //action here }); .fail(function() { //action here }); 我希望这是有道理的,因为我不知道这个过程的正确术语是什么:( 我正在使用jQuery,所以如果有更好的解决方案使用jQuery那么这很好。

Javascript Prototype General Enquries并按数组索引分配Id

我正在努力学习如何使用javascripts原型,我现在只是进入它。 如果我问一些荒谬的愚蠢问题,请原谅 我只有一些预先问题: 值得学习吗? 我的意思是它对我来说看起来像一个结构化/干净的方法? 你应该和jQuery一起使用吗? 是否有任何重大问题或理由不使用它,为什么不常用或者我只是慢? 实际问题: 我有以下代码: var BudgetSection = function BudgetSection(name ) { this.id = “”; this.name = name; this.monthlyTotal = 0.00; this.yearlyTotal = 0.00; this.subTotal = 0.00; this.lineItems = []; }; BudgetSection.prototype.calculateSubTotal = function() { this.subTotal = ((12 * this.monthlyTotal) + this.yearlyTotal); }; function BudgetLineItem(name) { this.id = “”; this.name = […]

jQuery / javascript事件 – 原型事件处理程序

以下代码不起作用,因为我直觉地期望它: function MyObject(input) { input.change(this._foo); this.X = undefined; } MyObject.prototype._foo = function() { alert(“This code is never called”); // but if it did this.X = true; } var test_input = $(“input#xyz”); // a random, existing input var m = MyObject(test_input); // attach handler (or try to) test_input.change(); // trigger event alert(mX); // undefined 我希望调用_foo() (如果发生this情况, […]

jquery如何返回所选元素的数组

我刚刚看到John Resig提出的谷歌技术讲座,他说jQuery作为一个arrays运行。 按照这个建议,我一直在玩一个子类arrays,它工作得很好但是我一直在查看jQuery源代码并且看不到他们使用了相同的方法 jQuery.prototype = new Array(); 我甚至无法看到它使用调用/ apply的原生Array.prototype.methods或窗口中的原型链。$ object,所以我想知道jQuery对象如何返回所选元素的数组。 我尝试过使用普通对象,但是如果返回一个数组就会停止链接命令的能力 如果可以从Array.prototype中获取一些返回数组的必要方法? 这就是我正在玩的东西。 ;(function(window, document, undefined){ function MyLib(){}; // prototype constructor functions function Core(){}; function Methods(){}; // create new instance of the MyLib object and call the construct method function myLib(selector){ return new MyLib().construct(selector); } // allow adding new methods to the prototype from the […]

为什么这个更改为Array原型在我的jQuery插件中不起作用?

我已将以下方法添加到Array原型中: Array.prototype.foreach = function(func){ for(var i = 0; i < this.length; i++){ if(!func(this[i]) === false) break; //return false from func in order to break the loop } return this; } 在同一个文件中, 在上面的代码之后,我有以下jQuery插件: jQuery.fn.addClassForEvents = function(){ var that = this; arguments.foreach(function(event){ that.bind(event[0], function(){ that.addClass(event[0]); }) .bind(event[1], function(){ that.removeClass(event[0]); }); }); return this; } 为了使用这个jQuery插件,我的代码看起来像: $(‘div’).addClassForEvents([‘mouseenter’, ‘mouseleave’]); […]

jQuery与原生原型冲突

我在使用jQuery和原生JavaScript( NOT prototype.js)时遇到了问题。 使用以下代码时,jQuery 1.9.1带有错误消息: Object.prototype.myVeryGreatFunction = function() { // … } [Error] TypeError: undefined is not a function (evaluating ‘U[a].exec(s)’) ft (jquery.min.js, line 4) wt (jquery.min.js, line 4) st (jquery.min.js, line 4) find (jquery.min.js, line 4) init (jquery.min.js, line 3) b (jquery.min.js, line 3) (anonymous function) (read.control.js, line 59) c (jquery.min.js, line 3) fireWith […]

在jQuery中扩展原型时,如何保持对this关键字的控制?

我在jQuery中实现了一个类似于类的结构,但是当我尝试调用我的一些函数时遇到了一些麻烦。 这是结构的设置方式: MyClass = function(name) { this.init(name); } $.extend(MyClass.prototype, { init: function(theName) { this.myFunction(); // works $(‘.myclass’).each(function(){ this.myFunction(); // doesn’t work }); }, myFunction = function(){} }); 我遇到的问题是,当我尝试从jQuery块(如上面的each()构造)中调用我的一个函数(例如, myFunction() )时,我得到错误“ myFunction() is not a function 。“ 我认为这与this关键字在jQuery块中更改其含义有关,但我不确定。 任何帮助,将不胜感激!

我试图将一个length()方法原型化为Object并打破jQuery – 怎么样?

我写了以下内容: Object.prototype.length = function(){ var count = -1; for(var i in this) count++; return count; } 有用。 但是当我执行我的页面时,即使不使用这个函数,Firebug告诉我jQuery的.appendTo()不再是一个函数。 为什么会这样?

处理jQuery事件时,’this’关键字在JavaScript类中重写

我用JavaScript在单个方法中定义了一个类: function MyClass(text) { this.text = text; } MyClass.prototype.showText = function() { alert(this.text); } 然后,我使用jQuery定义了一个充当click事件处理程序的方法: function MyClass(text) { this.text = text; $(‘#myButton’).click(this.button_click); } MyClass.prototype.showText = function() { alert(this.text); }; MyClass.prototype.button_click = function() { this.showText(); }; 当我点击按钮时,它无法说: 对象#没有方法’showText’ 似乎this在jQuery click事件处理程序中引用了HTML元素本身,并且它没有引用MyClass对象的实例。 我该如何解决这种情况? jsFiddle可用: http : //jsfiddle.net/wLH8J/