如何在jquery中将更新的表图标限制为仅单击的图标
How can I limit the table icon updated to only a clicked icon in jquery?
当我单击html表上的展开图标时,单击的父级会展开,但两个父级图标都会切换。如何防止切换两个父图标,并且只切换单击的父图标?请参阅下面的代码片段。谢谢
这是我的代码和演示:
$('td').addClass("clickh");
$('.hideme').find('div').hide();
$('.clickh').click(function() {
var img = $(".center img");
if (img.attr('src') == 'http://www.stemcor.com/images/plus.gif') {
// row expanded, so collapse...
img.attr('src', 'http://www.stemcor.com/images/minus.gif');
} else {
// row collapsed, so expand...
img.attr('src', 'http://www.stemcor.com/images/plus.gif')
}
$(this).parent().next('.hideme').find('div').slideToggle(500);
});
table {
width: auto;
border: 1px solid #aaa;
margin-bottom: 15px;
border-collapse: collapse;
}
th,
td {
width: auto;
border: 1px solid #aaa;
padding: 10px;
}
caption {
color: #222;
font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="adv-table">
<table cellpadding="0" cellspacing="0" border="0" class="display table table-bordered" id="hidden-table-info">
<thead>
<tr>
<th>expand</th>
<th>col 1</th>
<th>col 2</th>
<th>col 3</th>
<th>col 4</th>
<th>col 5</th>
<th>col 6</th>
<th>col 7</th>
<th>col 8</th>
<th>col 9</th>
</tr>
</thead>
<tbody>
<tr class="gradeA">
<td class="center">
<img src="http://www.stemcor.com/images/plus.gif" />
</td>
<td>item 1</td>
<td>item 2</td>
<td>item 3</td>
<td>item 4</td>
<td>item 5</td>
<td>item 6</td>
<td>item 7</td>
<td>item 8</td>
<td>Edit</td>
</tr>
<tr class="hideme">
<td colspan="10">
<div>
Sample te1t, sample text, sample text, sample text, sample text, sample text
</div>
</td>
</tr>
<tr class="gradeA">
<td class="center">
<img src="http://www.stemcor.com/images/plus.gif" />
</td>
<td>item 1</td>
<td>item 2</td>
<td>item 3</td>
<td>item 4</td>
<td>item 5</td>
<td>item 6</td>
<td>item 7</td>
<td>item 8</td>
<td>Edit</td>
</tr>
<tr class="hideme">
<td colspan="10">
<div>
Sample te1t, sample text, sample text, sample text, sample text, sample text
</div>
</td>
</tr>
</tbody>
</table>
</div>
只需使用
var img = $(this).children(".center img");
您单击的项目包含img:
$('td').addClass("clickh");
$('.hideme').find('div').hide();
$('.clickh').click(function() {
var img = $(this).children(".center img");
if (img.attr('src') == 'http://www.stemcor.com/images/plus.gif') {
// row expanded, so collapse...
img.attr('src', 'http://www.stemcor.com/images/minus.gif');
} else {
// row collapsed, so expand...
img.attr('src', 'http://www.stemcor.com/images/plus.gif')
}
$(this).parent().next('.hideme').find('div').slideToggle(500);
});
table {
width: auto;
border: 1px solid #aaa;
margin-bottom: 15px;
border-collapse: collapse;
}
th,
td {
width: auto;
border: 1px solid #aaa;
padding: 10px;
}
caption {
color: #222;
font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="adv-table">
<table cellpadding="0" cellspacing="0" border="0" class="display table table-bordered" id="hidden-table-info">
<thead>
<tr>
<th>expand</th>
<th>col 1</th>
<th>col 2</th>
<th>col 3</th>
<th>col 4</th>
<th>col 5</th>
<th>col 6</th>
<th>col 7</th>
<th>col 8</th>
<th>col 9</th>
</tr>
</thead>
<tbody>
<tr class="gradeA">
<td class="center">
<img src="http://www.stemcor.com/images/plus.gif" />
</td>
<td>item 1</td>
<td>item 2</td>
<td>item 3</td>
<td>item 4</td>
<td>item 5</td>
<td>item 6</td>
<td>item 7</td>
<td>item 8</td>
<td>Edit</td>
</tr>
<tr class="hideme">
<td colspan="10">
<div>
Sample te1t, sample text, sample text, sample text, sample text, sample text
</div>
</td>
</tr>
<tr class="gradeA">
<td class="center">
<img src="http://www.stemcor.com/images/plus.gif" />
</td>
<td>item 1</td>
<td>item 2</td>
<td>item 3</td>
<td>item 4</td>
<td>item 5</td>
<td>item 6</td>
<td>item 7</td>
<td>item 8</td>
<td>Edit</td>
</tr>
<tr class="hideme">
<td colspan="10">
<div>
Sample te1t, sample text, sample text, sample text, sample text, sample text
</div>
</td>
</tr>
</tbody>
</table>
</div>
https://jsfiddle.net/kisspa/ooqyd6h2
$('td').addClass("clickh");
$('.hideme').find('div').hide();
$('.clickh:first-child').click(function() {
var img = $(this).find('img');
if (img.attr('src') == 'http://www.stemcor.com/images/plus.gif') {
// row expanded, so collapse...
img.attr('src', 'http://www.stemcor.com/images/minus.gif');
} else {
// row collapsed, so expand...
img.attr('src', 'http://www.stemcor.com/images/plus.gif')
}
$(this).parent().next('.hideme').find('div').slideToggle(500);
});
相关文章:
- 单击更改图标并通过javascript添加一个css类
- 使用Angular单击时更改特定图示符图标的颜色
- 通过单击同一图标使菜单出现和消失
- 如何制作Yii框架的linkHtmlOptions's的CGridView显示为图像图标,单击时会触发javas
- 单击时切换上下图示符图标的问题
- 单击扩展图标时打开popup.html
- Jdewit Timepicker 在文本和图标单击上显示小部件
- 单击标记时更改标记图标(在单击事件上),并在单击另一个标记时将其恢复为普通图标
- 单击图标后编辑信息
- 添加/单击c#asp.netmvc5后禁用和更改按钮图标
- 如何在单击按钮时为整个星形图标上色
- 我是否可以在单击特定图标时更改地图标记图标,然后在单击另一个图标时切换,等等
- 如何在单击图标时渲染图标并调用函数
- 单击时如何更改汉堡菜单图标
- 单击href旁边的链接(删除图标)时删除href(Javascript帮助)
- 单击社交媒体图标(字体真棒)而未重定向到href
- 如果您单击文件夹图标而不仅仅是三角形图标,我如何让 jstree 关闭/打开
- 单击时使用 jquery 更改图标
- AngularJS在图标单击时隐藏引导日期选择器
- 如何在图标单击时激活拖放区