0
我想在單擊時在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'應該是唯一的!改用'class'。 – LinkinTED
是的,我知道id應該是唯一的!我想讓它看起來獨一無二,只有在
請仔細閱讀[mcve] - 特別是'minimal'部分 - 您的大部分代碼都標記爲「您可以忽略此部分」 - 如果我們可以忽略它,那麼它不應該成爲問題。這對於實際問題是什麼太令人困惑。你可以使用jsfiddle之類的東西重新創建問題。 –
回答
正如在評論中提到的
id
應該是唯一的。因此,爲了使這項工作,互換類,它會很好地工作:
來源
2016-07-25 10:32:52 LinkinTED
想你@Pilatus被建議使用相同的解決方案,它對我來說工作得很好! –
起初你應該知道
ID
應該是唯一的。處理Click
多個elements
事件時,請改爲使用class
。由於
jQuery
預計ID
是唯一的,它將始終在第一個元素上觸發。來源
2016-07-25 10:27:36 Pilatus
中創建它,它的工作原理完全符合我的需求! :) 謝謝!您的答案將被確認分享。 –
相關問題