在特定情况下使用jQuery/JS更改类
Change class with jQuery/JS in specific case
在这种情况下,我使用表。(后续架构)默认情况下,"tr.classified"显示为none。
我想添加一个按钮,当需要时显示"tr.classified"。
我不是网络开发人员。
我从这个开始;
<script>
$( "buttonchange" ).click(function() {
$( this ).switchClass( "tr.classified", "tr.classified2");
});
</script>
<div class="buttonchange">Display Archives</button>
根本不起作用:(
你能看看这个吗?
我想您正在jquery 中查找toggleClass
$( "#buttonchange" ).click(function() {
$( this ).toggleClass("classified classified2");
});
TOGGLECLASS
网上有很多关于jQuery
的toggleClass
函数使用的例子,我建议您查找一下。
同时,以下是如何解决您的问题:
- 将
toggle-master
类添加到要切换的行中 -
使用以下代码单击切换按钮时,在这些行上切换
classified
类:$("buttonchange").click(function(){$(".togle-master").togleClass("已分类");});
如果classified
是负责隐藏元素的类,则不需要classified2
类。
不要忘记在$(document).ready(function(){/*add here*/});
函数中添加代码,并在页面中包含jQuery
。
下面是一个工作示例。
好吧,你只需要为按钮添加一个事件处理程序,然后像这样显示或隐藏你的表,
HTML代码:
<button class="clik">Display/hide Table</button>
<table id="codexpl">
<tr>
<th>#</th>
<th>Columna</th>
<th>Relative</th>
<th>Isso</th>
</tr>
<tr>
<td>1</td>
<td>This</td>
<td>Column</td>
<td>Is</td>
</tr>
<tr>
<td>2</td>
<td>Coloumn</td>
<td>two</td>
<td>this</td>
</tr>
<tr>
<td>3</td>
<td>is</td>
<td>not equals</td>
<td>a</td>
</tr>
<tr>
<td>4</td>
<td>the</td>
<td>Column</td>
<td>real</td>
</tr>
<tr>
<td>5</td>
<td>first</td>
<td>One</td>
<td>Coloumn</td>
</tr>
</table>
Jquery代码:
//hide the table by default
$('#codexpl').hide();
$(".clik" ).on('click',function() {
$('#codexpl').toggle();
});
JSFiddle:
http://jsfiddle.net/dreamweiver/eboLopwc/1/
快乐编码:)
试试这个:-
$( "#buttonchange" ).click(function() { //put '#' here for 'id' selector
$( this ).toggleClass("classified classified2"); // use 'toggleClass' as shown here
});
相关文章:
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 点击facebook像素跟踪注册(JS/JQUERY代码)
- 用于发现哪个JS/jQuery脚本正在冻结页面的工具或技术
- 使用js/jQuery检查对象(而不是元素)是否真的存在
- 如何使用JS/jQuery在另一个网站上显示iframe中的特定内容
- 在坐标上模拟点击坐标js/jQuery
- 如何插入js/jquery变量isideHTML标记
- 我需要JS/jQuery中的倒计时计时器
- 当我使用JS/jQuery/any插件点击图像时,我如何使背景变暗+不可点击,并放大和居中我的图像
- JS/jquery:我将如何遍历HTML中的每个图像
- 非常奇怪的JS/jQuery行为-alert()包含/排除
- JS/Jquery - 如果输入具有数组的任何名称,则返回关联的值
- 使用 js/jQuery/css 在重力形式中覆盖 !important
- 如何在 js / jquery 中绑定“数据追加”事件
- 解析 RSS 命名空间媒体:缩略图 (js/jQuery)
- AJAX/page refresh kills JS/jQuery
- 带有post值的JS jQuery重定向,不带<表单>
- 改变与“"至“-"(JS/Jquery)
- 我正在尝试使用JS/JQuery自动对网站中的文本执行多个查找和替换操作
- 是否覆盖网站上文本输入区域的默认移动设备键盘?(HTML/JS/jQuery)