JQuery样式操作、click事件以及索引值-选项卡应用示例

2020-05-27 18:03:40易采站长站整理

<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var $btn=$('.btns input');
var $div=$('.cons div');
// alert($btn.length);
// alert($div.length);
$btn.click(function () {
// 我点击哪一个按钮,$(this)就指的是谁,而this
//指的是原生的,$(this)指的是JQuery的
// $(this).siblings().removeClass('current');
// $(this).addClass('current');//可以用链式调用
$(this).addClass('current').siblings().removeClass('current');
var num=$(this).index();
$div.eq($(this).index()).addClass('active').sibling().removeClass('active');
})
})
</script>
</head>
<body>
<div class="btns">
<input type="button" name="" value="01" class="current">
<input type="button" name="" value="02">
<input type="button" name="" value="03">
</div>
<div class="cons">
<div class="active">选项卡1的内容</div>
<div>选项卡2的内容</div>
<div>选项卡3的内容</div>
</div>
</body>
</html>

JQuery可以使用链式调用,在改变选项卡样式的时候就用到了。

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具 http://tools.jb51.net/code/HtmlJsRun 测试上述代码运行效果。

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery常见事件用法与技巧总结》、《jQuery常用插件及用法总结》、《jQuery操作json数据技巧汇总》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》及《jquery选择器用法总结》

希望本文所述对大家jQuery程序设计有所帮助。