Step by step to create a jQuery tabs plugin - 1
3907 단어 jquery
jQuery UI has a so-called “unified API” which uses the following syntaxfor invoking methods:
// call select method for tabs
$("ul.example").tabs("select", 1);
API methods are called by supplying the method name as a string followed by method arguments. To be honest, I think that this kind API design is fundamentally wrong. It has the following problems:
I dislike the jQuery UI ’s unified API either. There is another article supporting jQuery UI’s unified API:
With jQuery UI, all the plugins work with jQuery and it’s philosophy. Working withJohn Resig’s supervision and incite. Working together. Returning a seperate APIhas some potential, but not the way it is implimented here.
In my opinion, a component is a collection of nodes, properties, events and methods,which should be presented in his own instance not the DOM node in jQuery.
I love jQuery, but i think the components based on jQuery should be more like extjs,qooxdoo.
Maybe it’s time for me to learn how to write a jQuery plugin, and convert it tothe way used in jQuery Tools.
A simple jQuery tabs plugin
Html markup is the same as jQuery UI Tabs.
<div class="tabs">
<ul>
<li><a href="javascript:;" >Tab 1</a></li>
<li><a href="javascript:;" >Tab 2</a></li>
<li><a href="javascript:;" >Tab 3</a></li>
</ul>
<div>
Pane 1 content</div>
<div>
Pane 2 content</div>
<div>
Pane 3 content</div>
</div>
Let’s write some basic CSS rules:
.tabs ul
{
list-style-type: none;
padding: 0px;
margin: 0px;
}
.tabs li
{
display: inline;
}
.tabs li a
{
text-decoration: none;
}
.tabs a.current
{
background-color: #ccc;
}
And the jQuery plugin code:
(function($) {
$.fn.tabs = function() {
var tabs = this.children("ul").find("li > a");
var panes = this.children("div");
var current = 0;
function clickTab(index) {
current = index;
tabs.removeClass("current").eq(current).addClass("current");
panes.hide().eq(current).show();
}
clickTab(0);
tabs.click(function() {
clickTab(tabs.index(this));
});
return this;
};
})(jQuery);
The invoke code:
$(function() {
$("div.tabs").tabs();
});
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
jQuery 전후 예이 기사에서는 jquery after() 및 before() 메소드의 예를 볼 것입니다. before() 메서드는 선택한 요소 앞에 지정된 콘텐츠를 삽입합니다. after() 메서드는 선택한 요소 뒤에 지정된 콘텐츠...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.