删除单击“新建”按钮后添加的类
Remove class added after clicking new button
需要一些帮助。设法创建了一个jQuery来更改被点击后的按钮类,但我需要你点击一个新的按钮类。
我在努力,但做不到。我已经尝试过一些方法,但无论何时尝试,我都可以删除所有内容,并单击新类中不包含的按钮。
有人能帮我吗?
$(document).ready(function() {
$('div#filters-container button').click(function() {
var id_button = $(this).val();
$("#" + id_button + "").addClass("cbp-filter-item-active");
});
});
.cbp-filter-item-active {
background-color: #5d5d5d;
color: #fff!important;
border-color: #5d5d5d
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="filters-container">
<button value="0" id="0" href="videos2-carrega.asp">Todos</button>
<button value="1" id="1" href="videos2-carrega.asp?cat=1">Família</button>
<button value="2" id="2" href="videos2-carrega.asp?cat=2">Finanças</button>
<button value="3" id="3" href="videos2-carrega.asp?cat=3">Propaganda</button>
<button value="4" id="4" href="videos2-carrega.asp?cat=4">Empreendedorismo</button>
</div>
感谢您的关注。
$(document).ready(function() {
$('div#filters-container button').click(function() {
var id_button = $(this).val();
$("#"+id_button+"").addClass("cbp-filter-item-active");
$(this).siblings().removeClass('cbp-filter-item-active'); //added line
});
});
添加的代码行将删除其余按钮的类。演示:http://jsfiddle.net/8oytw0ue/2/
实际上,简单得多:
$(document).ready(function() {
$('div#filters-container button').click(function() {
$(this).addClass("cbp-filter-item-active");
$(this).siblings().removeClass('cbp-filter-item-active');
});
});
你不需要ID来选择按钮$(这个)就可以了…
$(document).ready(function() {
var $buttons = $('div#filters-container button').click(function() {
var $this = $(this).addClass("cbp-filter-item-active");
$buttons.not($this).removeClass("cbp-filter-item-active");
});
});
.cbp-filter-item-active {
background-color: #5d5d5d;
color: #fff!important;
border-color: #5d5d5d
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="filters-container">
<button value="0" id="0" href="videos2-carrega.asp">Todos</button>
<button value="1" id="1" href="videos2-carrega.asp?cat=1">Família</button>
<button value="2" id="2" href="videos2-carrega.asp?cat=2">Finanças</button>
<button value="3" id="3" href="videos2-carrega.asp?cat=3">Propaganda</button>
<button value="4" id="4" href="videos2-carrega.asp?cat=4">Empreendedorismo</button>
</div>
您可以将类添加到当前单击的元素,并从同级元素中删除它们:
$('div#filters-container button').click(function() {
$(this).addClass("cbp-filter-item-active").siblings().removeClass('cbp-filter-item-active');
});
工作演示
您可以使用这个:http://jsfiddle.net/620tqr19/
$('button').on('click',function(){
$('button').removeClass('active');
$(this).addClass('active');
});
根据要求更改class="active"。
相关文章:
- 在HTML5画布上添加按钮和控件
- 无法在图像周围添加按钮
- Sitecore富文本编辑器-添加按钮
- 如何在标记href中添加按钮
- jqgridnavgrid在搜索按钮和自定义按钮之后添加按钮
- 如何在视频标签中添加按钮覆盖
- 添加按钮以切换浏览器
- 如何在php中添加按钮而不是错误状态和成功消息
- Struts2jquery可编辑网格,要禁用内联添加按钮
- 如何在服务器端的文本中添加按钮单击事件
- 简单的dart网页应用程序在IE上添加按钮的速度非常慢
- 向HTML页面添加按钮,更新表格信息
- (PHP&JavaScript)a href添加按钮赢得'我不适用于Mozilla,但适用于IE和谷歌
- 单击添加按钮后如何显示答案按钮
- 如何在 extjs 5 中的工具提示中添加按钮
- 在数据表的新行中添加按钮,变得未定义
- 如何向照片滑动添加按钮
- 动态添加按钮后刷新 jQm 标头
- PHP,MySql和JQuery - 按添加按钮后填充选择框
- 动态添加按钮,这些按钮也会动态添加文本框 AngularJS