Sharing of tab production technology for jquery plug-in development

小云云
Release: 2017-12-29 17:53:30
Original
1367 people have browsed it

This article mainly introduces the tab production of jquery plug-in development in detail. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

In jquery, common plug-in development methods include:

One is to extend a method for the $ function itself. This is a static extension (also called a class extension). This kind of plug-in is usually Tool method,

There is also one that extends on the prototype object $.fn. The developed plug-in is used on the dom element

1. Class level extension


$.showMsg = function(){ alert('hello,welcome to study jquery plugin dev'); } // $.showMsg();
Copy after login

Be careful to introduce the jquery library in advance. In the above example, a method showMsg is added to the $function, then it can be called with $.showMsg()


$.showName = function(){ console.log( 'ghostwu' ); } $.showName();
Copy after login

This kind of plug-in is relatively rare and is generally used to develop tool methods, such as $.trim, $.isArray(), etc. in jquery

2. Expand the function on $.fn.

This kind of plug-in is used on elements. For example, if I extend a function and click a button, the value of the current button will be displayed.


$.fn.showValue = function(){ return $(this).val(); } $(function(){ $("input").click(function(){ // alert($(this).showMsg()); alert($(this).showMsg()); }); }); 
Copy after login

Add a showValue method on $.fn to return the value of the current element. After obtaining the page input element and binding the event, you can call this method to display the button The value "click me" is commonly used in actual plug-in development. Next, we will use this extension mechanism to develop a simple tab plug-in:

Page layout and style:


      Document     

作者:ghostwu(1)

博客: //m.sbmmt.com/ghostwu/

作者:ghostwu(2)

博客: //m.sbmmt.com//ghostwu/

作者:ghostwu(3)

博客: //m.sbmmt.com//ghostwu/

Copy after login

tab2.js file


;(function ($) { $.fn.tabs = function (opt) { var def = { evType: "click" }; //定义了一个默认配置 var opts = $.extend({}, def, opt); var obj = $(this); $("ul li:first", obj).addClass("active"); obj.children("p").hide(); obj.children("p").eq(0).show(); $("ul li", obj).bind(opts.evType, function () { $(this).attr("class", "active").siblings("li").attr("class",""); var id = $(this).find("a").attr("href").substring(1); obj.children("p").hide(); $("#" + id, obj).show(); }); }; })(jQuery);
Copy after login

1, a self-executing function that encapsulates the plug-in into a module and converts the jQuery object Pass to the formal parameter $

2, line 3, to define a default configuration, the trigger type of the tab, the default is click event

3, line 4, if opt passes the parameter, then Use the opt configuration, otherwise use the default configuration in line 3. The purpose of this line is to configure the plug-in’s representation without changing the plug-in source code

4, lines 7-9 , let the first p of the tab be displayed, and hide the rest. Add class='active' to the first tab. Highlight

5, lines 11-16, in yellow, and click the corresponding tab. , let the corresponding p show and hide

Related recommendations:

About JavaScript plug-in Tab effect sharing

implementation WeChat applet with tab function

JS+jQuery example of writing a simple tab

The above is the detailed content of Sharing of tab production technology for jquery plug-in development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!