Home Web Front-end JS Tutorial Detailed explanation of complete examples of component development using jQuery

Detailed explanation of complete examples of component development using jQuery

Mar 18, 2017 pm 03:01 PM

The principles of using jQuery for component development and using pure JavaScript scripts (without using a framework) are basically similar, especially the organization of public methods is the same.

The difference is that jQuery uses a plug-in mechanism to directly bind the operation object (DOM element) through $(), and then bind events to the DOM element or HTML code.

Another difference is that jQuery is used as a tool to create DOM objects, quickly find specified DOM objects, etc.

The example test passed.

Basic simple example only implements the functions of adding pages and selecting pages.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Design JS component with jQuery </title>
<script src="mx/scripts/lib/jquery.js" type="text/javascript"></script>
<link href="tabs.css" rel="stylesheet" type="text/css" />

 <style>
      
.tabsp{width: 500px;height: 350px;margin-top: 0px;margin-left: 0px;}
.tabsp ul{
	width: 500px;height: 20px;
	list-style: none;
	
	margin-bottom: 0px;margin: 0px;
	padding: 0px;
	border-left:solid 1px #ffffff;border-right:solid 1px #ffffff;border-top:solid 1px #ffffff;border-bottom:solid 1px #e0e0e0;
}

.tabsp p{
	width: 500px;height: 330px;
	background-color: #ffffff; 
	border:solid 1px #e0e0e0;
}


.tabsSeletedLi{
	width: 100px;height: 20px;
	background-color: white;
	float: left;
	text-align: center;
	border-left:solid 1px #e0e0e0;border-right:solid 1px #e0e0e0;border-top:solid 1px #e0e0e0;border-bottom:solid 1px #ffffff;
}

.tabsSeletedLi a{
	width: 100px;
	height: 20px;
	color:#000000;
	text-decoration:none;
}

.tabsUnSeletedLi{
	width: 100px;height: 20px;
	background-color: #e0e0e0; 
	float: left;
	text-align: center;
	border:solid 1px #e0e0e0;
}

.tabsUnSeletedLi a{
	width: 100px;height: 20px;
	color: #ffffff;
	text-decoration:none;
}
 
 </style>   
</head>

<body>
<!--
	<p style="width:400px;height:100px;border:solid 1px #e0e0e0;">
	
	</p>
-->

    <!--tabs示例-->
    <p id="mytabs">
        <!--选项卡区域-->
        <ul>
            <li><a href="#tabs1">选项1</a></li>
            <li><a href="#tabs2">选项2</a></li>
            <li><a href="#tabs3">选项3</a></li>
        </ul>
        <!--面板区域-->
        <p id="tabs1">11111</p>
        <p id="tabs2">22222</p>
        <p id="tabs3">33333</p>
    </p>
    
<script lang="javascript">
(function ($) {
    $.fn.tabs = function (options) {
    	var me = this;
    
		//使用鼠标移动触发,亦可通过click方式触发页面切换
        var defualts = { switchingMode: "mousemove" };

		//融合配置项
        var opts = $.extend({}, defualts, options);

		//DOM容器对象,类似MX框架中的$e
        var $e = $(this);
        
        //选中的TAB页索引
        var selectedIndex = 0;
        
        //TAB列表
        var $lis;

		//PAGE容器
		var aPages = [];


		//初始化方法
		me.init = function(){
		
			//给容器设置样式类
	        $e.addClass("tabsp");	 
	        
	        $lis = $("ul li", $e);
	        
	        //设置TAB头的选中和非选中样式
	        $lis.each(function(i, dom){
	        	if(i==0){
	        		$(this).addClass("tabsSeletedLi")
	        	}else{
	        		$(this).addClass("tabsUnSeletedLi");
	        	}
	        
	        });
	        
	       	//$("ul li:first", $e).addClass("tabsSeletedLi");
	        //$("ul li", $e).not(":first").addClass("tabsUnSeletedLi");
			//$("p", $e).not(":first").hide();
			
			//TAB pages绑定
			var $pages = $(&#39;p&#39;, $e);
			$pages.each(function(i, dom){
				if(i == 0){
					$(this).show();
				}else{
					$(this).hide();
				}
				
				aPages.push($(this));		
			});
	        
	        
	        
			//绑定事件
	        $lis.bind(opts.switchingMode, function() {
	        	var idx = $lis.index($(this))
	            me.selectPage(idx);
	        });		
		
		}
		

		/**
		 *  选中TAB页
		 *
		 */
		me.selectPage = function(idx){
			if (selectedIndex != idx) {	                
                $lis.eq(selectedIndex).removeClass("tabsSeletedLi").addClass("tabsUnSeletedLi");
                $lis.eq(idx).removeClass("tabsUnSeletedLi").addClass("tabsSeletedLi");
                
                aPages[selectedIndex].hide();
	            aPages[idx].show();
                selectedIndex = idx;
            };
		}
        
        
        me.showMsg = function(){
        	alert(&#39;WAHAHA!&#39;);
        }
        
		//自动执行初始化函数
		me.init();
		
		//返回函数对象
		return this;
    };
})(jQuery);

</script>   

<script type="text/javascript">
/*
    $(function () {
        $("#mytabs").tabs;
    });
*/


	var tab1 =  $("#mytabs").tabs();
	tab1.showMsg();
</script> 
</body>
</html>
Copy after login

The final effect is as shown in the picture:


The above is the detailed content of Detailed explanation of complete examples of component development using jQuery. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

Introduction to how to add new rows to a table using jQuery Introduction to how to add new rows to a table using jQuery Feb 29, 2024 am 08:12 AM

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:

What are the characteristics of the bootstrap framework? What are the characteristics of the bootstrap framework? Apr 05, 2024 am 01:21 AM

Features of the Bootstrap framework: mobile-first design, providing responsive layout. Flexible grid system to easily create multi-column layouts. Rich component library including common UI elements. Customizable themes that support a high degree of customization. Cross-browser compatibility ensures that the website displays properly on different devices. Lightweight and modular, containing only the components you need. Support component development and improve development efficiency. Provide starting templates to quickly start projects.

See all articles