0
我有一個動態創建的表,行類爲「rowclass」和id,如1,2,3,4等。該行內有一個鏈接。 我想觸發該鏈接,例如在該行的任何地方點擊該鏈接,即a
。這是HTML。如何在該行的任何位置點擊時觸發點擊該「鏈接」?
<tr id="40" data-id="40" data-parent="" class="rowclass act-tr-collapsed act-tr-level-0" data-level="0">
<td id="235" style="font-weight:bold;font-size:17px;width:40%;">
<a href="javascript:void(0)" class="act-more act-collapsed"><span class="i">+ </span></a>Nametobeappended<span id="s40" class="icon icon-info"
</span>
</td>
<td id="236">
<div style="height: 20px;position: relative;">
<div id="d236" style="height: 20px;float:left;color: #ffffff; font-weight: bold;font-family: Arial Black; width:50%;background-color: #d00000;">
<div style="padding-left:5px;z-index: 99;position: absolute;">50</div>
<div style="float:right; height: 20px; background-color: rgb(102,197,232);width:0%;position: absolute; top:0; left:50%;"></div>
</div>
</div>
</td>
<td id="237">
<div style="height: 20px;position: relative;">
<div id="d237" style="height: 20px;float:left;color: #ffffff; font-weight: bold;font-family: Arial Black; width:50%;background-color: #d00000;">
<div style="padding-left:5px;z-index: 99;position: absolute;">50</div>
<div style="float:right; height: 20px; background-color: rgb(102,197,232);width:0%;position: absolute; top:0; left:50%;"></div>
</div>
</div>
</td>
<td id="238">
<div style="height: 20px;position: relative;">
<div id="d238" style="height: 20px;float:left;color: #ffffff; font-weight: bold;font-family: Arial Black; width:50%;background-color: #d00000;">
<div style="padding-left:5px;z-index: 99;position: absolute;">50</div>
<div style="float:right; height: 20px; background-color: rgb(102,197,232);width:0%;position: absolute; top:0; left:50%;"></div>
</div>
</div>
</td>
</tr>
這是我寫的JQuery不起作用。
$('.rowclass').on("click", function(){
idss = $(this).children().attr('id'); //td id
$("#"+idss).find('a').trigger('click'); //want to click <a> of that particular row
});
console.log("id: "+idss)// says idss undefined
$("#"+idss).find('a').click(); //doesn't work
什麼是點擊事件觸發?它看起來像你試圖在每個單元中找到錨標籤? – Simon
@Simon Yeah在html中給出的那個'
如果以下提供的解決方案都不適合您的目的,請告訴我,我會看看它 – Simon
回答
來觸發特定行的定位標記的onclick該行的,這是什麼工作:
});
這可能不是最好的解決方案。我是新來的編程,所以如果有更好的方法,我會很高興知道它。乾杯!
來源
2017-04-12 19:28:08
如果你創建動態的行,你需要選擇他們這樣說:
的jsfiddle例如:https://jsfiddle.net/Hulothe/1u8scath/1/
來源
2017-04-12 13:55:46 Hulothe
我想我不清楚。點擊該特定行,我想觸發'' –
@AyushiZile的點擊對不起,我應該已經制定了更多的答案。我編輯了我的帖子。 – Hulothe
感謝@Hulothe的答案! –
要做到這一點使用:
我認爲問題在於事件綁定到元素後加載表。 $(document).on('click','element',function);確保事件總是被綁定,甚至在加載html之前。
或者使用.children返回多個元素,所以不能提供一個id。
來源
2017-04-12 13:56:38
我不明白e實際上是什麼?此外,該表首先被加載,然後我輸入上面寫的js。那是錯誤的地方嗎? –
LE:
如果(e.target ===鏈接[0]) - 在這裏,我們檢查,如果點擊的元素是一個標籤本身。如果是這樣,我們會讓默認行爲發生。 如果沒有,我們觸發標籤點擊
來源
2017-04-12 14:03:06 Vladut
雖然此代碼片段可能是解決方案,但[包括解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-基於代碼的答案)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – Adam
@Vladut這不起作用,因爲它會出現以下錯誤:「未捕獲的RangeError:超出最大調用堆棧大小」。如果我在循環中有'console.log'任何東西,它就會進入一個無限循環 –
是的,對不起。它並沒有跨越我的想法觸發一個孩子的點擊事件,它也將被視爲點擊父母。這就是錯誤所說的。 此代碼應該可以正常工作: jQuery(document).ready(function(){0} {0} {0} jQuery(document).on('click','.rowclass',function(e)var link = $(this)。找到( 「A」);如果 (e.target ===鏈接[0])返回false; link.trigger( '點擊'); 返回FALSE; });} ) – Vladut
相關問題