JS实现选项卡插件的两种写法(jQuery和class)
本文详细介绍两种JavaScript选项卡插件的实现方式。首先通过原生JS梳理基础逻辑,随后分别展示基于jQuery.fn.extend的扩展写法以及利用ES6 class类的面向对象写法。重点解析参数传递、事件绑定及DOM操作,帮助开发者构建更完善的交互组件。
本文分享了JavaScript实现选项卡插件的两种具体写法,供读者参考。
插件开发注意事项
在开发此类插件时,需注意以下几个关键点:
第一,需定义固定的HTML结构,例如选项卡标题使用li标签,内容区域使用div标签;第二,提前确定选中状态的样式;第三,建议先使用基础JS实现功能,再封装为插件模式。
HTML结构示例如下:
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
#tabBox {
box-sizing: border-box;
margin: 20px auto;
width: 500px;
}
.navBox {
display: flex;
position: relative;
top: 1px;
}
.navBox li {
box-sizing: border-box;
margin-right: 10px;
padding: 0 10px;
line-height: 35px;
border: 1px solid #999;
cursor: pointer;
}
.navBox li.active {
border-bottom-color: #FFF;
}
#tabBox>div {
display: none;
box-sizing: border-box;
padding: 10px;
height: 150px;
border: 1px solid #999;
}
#tabBox>div.active {
display: block;
}
</style>
<div id="tabBox">
<ul class="navBox">
<li class="active">编程</li>
<li>读书</li>
<li>运动</li>
</ul>
<div class="active">编程使我快乐</div>
<div>读书使我幸福</div>
<div>运动使我健康</div>
</div>
首先使用原生JS实现选项卡功能:
let len = liList.length;
for(let i = 0; i < len; i++) {
liList[i].index = i;
liList[i].onclick = function() {
for(let j = 0; j < len; j++) {
if(j === this.index) {
liList[j].className = 'active';
contentList[j].className = 'active';
}
else{
liList[j].className = '';
contentList[j].className = '';
}
}
};
}
jQuery扩展实现
插件实现的第一种方法:基于jQuery。
通过$.fn.extend方法,在jQuery原型上扩展选项卡功能:
(function($){
function clickLi() {
let $this = this,
$navBox = $this.find('.navBox'),
$liList = $navBox.find('li'),
$contentList = $this.find('div');
$liList.click(function(){
let $this = $(this),
index = $this.index();
$this.addClass('active').siblings().removeClass('active');
$contentList.eq(index).addClass('active').siblings().removeClass('active');
});
}
$.fn.extend({
tabClick: clickLi
});
})(jQuery);
具体使用方法如下:
let $tabBox = $('#tabBox');
$tabBox.tabClick();
ES6 Class实现
插件实现的第二种方法:基于class。
利用ES6中的class类,创建名为Tab的选项卡类,添加相应属性和方法,并支持多参数传递:
(function(){
class Tab {
constructor(selector, options) {
// 处理第一个参数
if(!selector)
throw new ReferenceError('The first selector parameter must be passed~~');
if(typeof selector === 'string')
this.container = document.querySelector(selector);
else if(selector.nodeType)
this.container = selector;
this.initialParams(options);
this.navBox = this.container.querySelector('.navBox'),
this.liList = this.navBox.querySelectorAll('li'),
this.contentList = this.container.querySelectorAll('div'),
this.count = this.liList.length;
this.change();
this.handleLi();
}
// 初始化参数
initialParams(options) {
let _default = {
showIndex: 0,
triggerEvent: 'click'
};
for(let key in options) {
if (!options.hasOwnProperty(key)) break;
_default[key] = options[key];
}
// 把信息挂载到实例上
for (let key in _default) {
if (!_default.hasOwnProperty(key)) break;
this[key] = _default[key];
}
}
// 切换标题
change() {
[].forEach.call(this.liList, (item, index) => {
if(index === this.showIndex) {
this.liList[index].className = 'active';
this.contentList[index].className = 'active';
return;
}
this.liList[index].className = '';
this.contentList[index].className = '';
});
}
// 绑定标题对应的事件
handleLi() {
[].forEach.call(this.liList, (item, index) => {
item.addEventListener(this.triggerEvent, () => {
this.showIndex = index;
this.change();
});
});
}
}
window.Tab = Tab;
})();
具体使用方法如下:
new Tab('#tabBox', {
showIndex: 2,
triggerEvent: 'mouseenter'
});
第二种方法是当前常用的实现方式,因其支持灵活传递参数。可根据需求设置默认参数,使选项卡插件更加完善。
如需深入学习,可参考javascript选项卡操作方法汇总与jquery选项卡操作方法汇总。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。
JS split切割后出现空字符串怎么过滤
2026-11-05 16:00:30
在 React 或 Vue 项目中封装使用 JS 节流防抖
2026-11-05 15:27:29
JavaScript中怎么将带小数的字符串转为整数?
2026-11-05 14:32:38
怎么用JavaScript策略模式重构电商促销计算逻辑?
2026-11-05 14:01:09
JS使用constructor判断数据类型可靠吗
2026-11-05 13:49:41
