我去年建立了一个网站,并使用了其他人编写的 MooTools 对象来提供选项卡功能。我从来没有花时间学习 MooTools。
从那时起,我学习了 JQuery,并意识到还有更多可用的插件,我决定继续使用它。但是,我还没有学到可以编写自己的插件的程度。
长话短说,我想将 MooTools 选项卡代码转换为 JQuery。我真的不想把 MooTools 学习到我可以自己做的程度,因为我决定根本不使用它。我用this tool经常将 C# 代码转换为 VB。 MooTools 是否有与 JQuery 等效的工具?
如果没有,我将发布我尝试从 MooTools 转换为 JQuery 的代码。这是使用它的示例页面:http://www.foldingchairdepot.com/p/National-Public-Seating-600-Series-Blow-Molded-Resin-Plastic-Folding-Chair---Set-of-4---Gray__NPS-602.aspx .
/**
* SimpleTabs - Unobtrusive Tabs with Ajax
*
* @example
*
* var tabs = new SimpleTabs($('tab-element'), {
* selector: 'h2.tab-tab'
* });
*
* @version 1.0
*
* @license MIT License
* @author Harald Kirschner <mail [at] digitarald.de>
* @copyright 2007 Author
*/
var SimpleTabs = new Class({
Implements: [Events, Options],
/**
* Options
*/
options: {
show: 0,
selector: '.tab-tab',
classWrapper: 'tab-wrapper',
classMenu: 'tab-menu',
classContainer: 'tab-container',
onSelect: function(toggle, container, index) {
toggle.addClass('tab-selected');
container.setStyle('display', '');
},
onDeselect: function(toggle, container, index) {
toggle.removeClass('tab-selected');
container.setStyle('display', 'none');
},
onRequest: function(toggle, container, index) {
container.addClass('tab-ajax-loading');
},
onComplete: function(toggle, container, index) {
container.removeClass('tab-ajax-loading');
},
onFailure: function(toggle, container, index) {
container.removeClass('tab-ajax-loading');
},
onAdded: Class.empty,
getContent: null,
ajaxOptions: {},
cache: true
},
/**
* Constructor
*
* @param {Element} The parent Element that holds the tab elements
* @param {Object} Options
*/
initialize: function(element, options) {
this.element = $(element);
this.setOptions(options);
this.selected = null;
this.build();
},
build: function() {
this.tabs = [];
this.menu = new Element('ul', {'class': this.options.classMenu});
this.wrapper = new Element('div', {'class': this.options.classWrapper});
this.element.getElements(this.options.selector).each(function(el) {
var content = el.get('href') || (this.options.getContent ? this.options.getContent.call(this, el) : el.getNext());
this.addTab(el.innerHTML, el.title || el.innerHTML, content);
}, this);
this.element.empty().adopt(this.menu, this.wrapper);
if (this.tabs.length) this.select(this.options.show);
},
/**
* Add a new tab at the end of the tab menu
*
* @param {String} inner Text
* @param {String} Title
* @param {Element|String} Content Element or URL for Ajax
*/
addTab: function(text, title, content) {
var grab = $(content);
var container = (grab || new Element('div'))
.setStyle('display', 'none')
.addClass(this.options.classContainer)
.inject(this.wrapper);
var pos = this.tabs.length;
var evt = (this.options.hover) ? 'mouseenter' : 'click';
var tab = {
container: container,
toggle: new Element('li').grab(new Element('a', {
href: '#',
title: title
}).grab(
new Element('span', {html: text})
)).addEvent(evt, this.onClick.bindWithEvent(this, [pos])).inject(this.menu)
};
if (!grab && $type(content) == 'string') tab.url = content;
this.tabs.push(tab);
return this.fireEvent('onAdded', [tab.toggle, tab.container, pos]);
},
onClick: function(evt, index) {
this.select(index);
return false;
},
/**
* Select the tab via tab-index
*
* @param {Number} Tab-index
*/
select: function(index) {
if (this.selected === index || !this.tabs[index]) return this;
if (this.ajax) this.ajax.cancel().removeEvents();
var tab = this.tabs[index];
var params = [tab.toggle, tab.container, index];
if (this.selected !== null) {
var current = this.tabs[this.selected];
if (this.ajax && this.ajax.running) this.ajax.cancel();
params.extend([current.toggle, current.container, this.selected]);
this.fireEvent('onDeselect', [current.toggle, current.container, this.selected]);
}
this.fireEvent('onSelect', params);
if (tab.url && (!tab.loaded || !this.options.cache)) {
this.ajax = this.ajax || new Request.HTML();
this.ajax.setOptions({
url: tab.url,
method: 'get',
update: tab.container,
onFailure: this.fireEvent.pass(['onFailure', params], this),
onComplete: function(resp) {
tab.loaded = true;
this.fireEvent('onComplete', params);
}.bind(this)
}).setOptions(this.options.ajaxOptions);
this.ajax.send();
this.fireEvent('onRequest', params);
}
this.selected = index;
return this;
}
});
最佳答案
简短的回答是否定的,没有任何“转换器”可以获取 motools 代码并使其为 jQuery 做好准备。
但是,我强烈建议使用类似 jQuery UI 选项卡功能的解决方案:http://jqueryui.com/demos/tabs/ 。它的实现非常简单,并且有很多选项的强大支持。
如果您仍然决定自己动手,jQuery 上有一个非常直接的教程(截屏视频),供设计师使用,它将教您基础知识。这就是我学到的(重新发明轮子没有用):http://jqueryfordesigners.com/jquery-tabs/
关于javascript - 有没有一种简单的方法可以将 MooTools 代码转换为 JQuery 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4415819/