当$(this)单击时,在每个tr中单独执行onclick函数
Execute an onclick function alone in every tr when $(this) clicked?
我想在点击tr时在tr中的每个项目中执行相同的代码。所有tr元素中的项目都相同。所以问题是,脚本没有发现任何遗漏,并执行了使用这个id 找到的第一个元素
这是我需要的HTML代码:
<tbody id="bList">
<tr class="">
<td class="center" style="display:none;"></td>
<td></td>
<td class="center"></td>
<td class="center"></td>
<td class="center" style="width:200px !important;">
<span id="timer" class="countdown callback ended" onclick="return false;"
data-inc="0" data-time="10" data-id="btc2" rel="nofollow"></span>
<span id="starter" class="" active="yes"> > </span>
</td>
<td class="center">
<span id="open" class="">Open</span>
</td>
<td class="center"></td>
<td class="center"></td>
</tr>
<tr class="selected">
<td class="center" style="display:none;"></td>
<td></td>
<td class="center"></td>
<td class="center"></td>
<td class="center" style="width:200px !important;">
<span id="timer" class="countdown callback ended" onclick="return false;"
data-inc="0" data-time="10" data-id="btc1" rel="nofollow"></span>
<span id="starter" class="" active="no"> > </span>
</td>
<td class="center">
<span id="open" class=""> Open </span>
</td>
<td class="center"></td>
<td class="center"></td>
</tr>
</tbody>
为了在单击时将选定的类添加到tr中,我将在jquery中使用.siblings()函数。但我试着用它来执行同样的通缉令,但我没有想到。
这是我尝试过的脚本:
$('#open').click(function(){
/*$(this).parent().parent().find('td:nth-child(5) #starter')
.html('Start Timer! >');*/
if($(this).parent().parent().find('#timer').hasClass('ended'))
{
$(this).parent().parent().find('#starter').html('Start Timer! >')
.attr({'active':'yes'});
}else{
//You can ignore this part. It does display a modal.
if($('#starterModal').is(':hidden')){
$('#starterModal').show().wait(6000).hide();
}
}
});
$('#starter').click(function(){
if($(this).attr('active') == "yes"){
$(this).parent().find('#timer').click();
$(this).html('>').attr({'active':'no'});
//Update rewards taken
console.log($(this).parent().parent().find('td:nth-child(1)').html());
$.ajax({
//You can ignore it either. It's working fine.
type : 'POST',
url : '../files/php/update_rewardHistory.php',
data : {id : $(this).parent().parent().find('td:nth-child(1)').html(),
currency : "btc"
},
success : function(data){}
});
}else{
//You can ignore this part! It does display a modal.
if($('#starterModal').is(':hidden')){
$('#starterModal').show().wait(6000).hide();
}
}
});
$('#bList tr').click(function(){
$(this).addClass('selected').siblings().removeClass('selected');
//console.log($(this).parent().parent().find('td:nth-child(1)').html());
});
那么,有办法做到这一点吗?
首先,您应该知道ID
应该是唯一的。在多个elements
上处理Click
事件时,请改用class
。
它将始终在第一个元素上激发,因为jQuery
期望ID
是唯一的。
$('.starter').click(function(){
//code
})
如注释中所述,id
应该是唯一的
因此,要实现这一点,请切换到类,它会很好地工作:
$('.open').click(function() {
var tr = $(this).closest('tr') ,
timer = tr.find('.timer') ,
starter = tr.find('.starter');
if( timer.hasClass('ended')) {
starter.html('Start Timer! >').attr({
'active': 'yes'
});
} else {
if ($('#starterModal').is(':hidden')) {
$('#starterModal').show().wait(6000).hide();
}
}
});
$('.starter').click(function() {
var elem = $(this),
tr = elem.closest('tr'),
timer = tr.find('.timer');
if(elem.attr('active') == "yes") {
timer.click();
elem.html('>').attr({
'active': 'no'
});
//update rewards taken
console.log(tr.find('td:nth-child(1)').html());
/*$.ajax({ //you can ignore it also! it's working fine!
type: 'POST',
url: '../files/php/update_rewardHistory.php',
data: {
id: $(this).parent().parent().find('td:nth-child(1)').html(),
currency: "btc"
},
success: function(data) {}
});*/
} else { //you can ignore this part! do display a modal
if ($('#starterModal').is(':hidden')) {
$('#starterModal').show().wait(6000).hide();
}
}
});
$('#bList tr').click(function() {
$(this).addClass('selected').siblings().removeClass('selected');
//console.log($(this).parent().parent().find('td:nth-child(1)').html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="">
<td class="center" style="display:none;"></td>
<td></td>
<td class="center"></td>
<td class="center"></td>
<td class="center" style="width:200px !important;">
<span class="timer countdown callback ended" onclick="return false;" data-inc="0" data-time="10" data-id="btc2" rel="nofollow"></span>
<span class="starter" active="yes"> > </span>
</td>
<td class="center">
<span class="open">Open</span>
</td>
<td class="center"></td>
<td class="center"></td>
</tr>
<tr class="selected">
<td class="center" style="display:none;"></td>
<td></td>
<td class="center"></td>
<td class="center"></td>
<td class="center" style="width:200px !important;">
<span class="timer countdown callback ended" onclick="return false;" data-inc="0" data-time="10" data-id="btc1" rel="nofollow"></span>
<span class="starter" active="no"> > </span>
</td>
<td class="center">
<span class="open"> Open </span>
</td>
<td class="center"></td>
<td class="center"></td>
</tr>
</tbody>
</table>
相关文章:
- 在单独的ul's
- Javascript排序的图像弹出窗口..可以't单独弹出
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- Jquery - table.row(tr) is undefined
- 用Javascript添加带有#text的tr元素
- 将单独的数组深层键转换为所需的类型(数组或对象)
- 从单独的html文件预览Javascript文本
- 使用Javascript,如何显示<tr>在另一<tr>悬停时,隐藏第二个<tr>
- 有什么“;错误的”;将javascript放在asp.net表单上,而不是放在单独的文件中
- 如何将要单独填充的每个HTML画布路径/形状分开
- 如何使jQuery中的悬停函数单独应用于数据库映像
- 如何在foreach循环中对每个产品单独应用评级系统
- 如何从表中的api中删除表tr数据comping
- Javascript单独的游戏窗口
- 当两个单独的单词被放在目标上时,使用Jquery获取一个值
- drawimage到一个单独的画布宽度和高度
- 访问对象的最简单方法'的单独财产
- (取消)使用单独的复选框激活复选框列表
- 将表 td 动态分解为单独的 tr
- 当$(this)单击时,在每个tr中单独执行onclick函数