原型相当于没有美元的jquery函数

时间:2014-04-14 06:10:42

标签: javascript prototypejs

我有一个包含jQuery 1.8和Prototype 2的网站,使用$(美元符号)似乎调用jQuery但我想调用Prototype(来自控制台)。特别要做这样的事情:How to find event listeners on a DOM node when debugging or from the JavaScript code?

我在原型中有一个等效的jQuery()函数吗?

1 个答案:

答案 0 :(得分:1)

Prototype不是像jQuery这样的单个对象,所以你不能像那样调用它。

Jquery的

var jq = $('#id')

此时你有一个引用DOM的jQuery对象。但是,如果您尝试将其用作实际的DOM对象,则无法使用它。

// This won't work
jq.className = "";
// This works because it's referencing the function inside jQuery
jq.removeClass();

请注意,如果需要,jQuery可以为您提供实际的DOM元素,但这不是默认行为。

原型

你需要将Prototype视为使基础Javascript更好而不是对象。一个很好的例子是Object.isUndefined。 {J}中已经存在Object对象,Prototype只是用另一个函数扩展它。当你看到Prototype表现得像jQuery时,它几乎总是因为Prototype扩展了已经存在的东西

// This is a DOM reference
var pro = $('id'); // equivalent to document.getElementById('id');
pro.className = "";
// But there's no base Prototype object so this fails
pro.isUndefined();
// This is correct
Object.isUndefined(pro);