2017-04-12 76 views
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 
+0

什麼是點擊事件觸發?它看起來像你試圖在每個單元中找到錨標籤? – Simon

回答

0

來觸發特定行的定位標記的onclick該行的,這是什麼工作:

$(document).on('click', '.rowclass', function() { 
    $(this).children().find('a span').trigger('click'); 
    //go to td then find <a> <span> to trigger click 

});

這可能不是最好的解決方案。我是新來的編程,所以如果有更好的方法,我會很高興知道它。乾杯!

1

如果你創建動態的行,你需要選擇他們這樣說:

的jsfiddle例如:https://jsfiddle.net/Hulothe/1u8scath/1/

$(document).ready(function() { 
    $(document).on('click', 'tr.rowclass', function() { 
     alert('o'); 
     $(this).children().find('a').trigger('click'); //want to click <a> of that particular row 
    }); 

    // And you need to handle a click event on the `<a>` if you want to trigger a click on it, like this: 

    $(document).on('click', 'a', function(e) { 
     e.stopPropagation(); 
     alert('Clicked'); 
    }); 
}); 
+0

我想我不清楚。點擊該特定行,我想觸發'' –

+0

感謝@Hulothe的答案! –

0

要做到這一點使用:

$(document).on('click', '.rowclass', function(e) { 
    // e.target is the element you clicked on 
    // .closest('tr') finds the closest parent that is an 'tr' element 
    // .find('a') finds the child that is an 'a' element 
    // .trigger('click') triggers a click on the found element 

    $(e.target).closest('tr').find('a').trigger('click'); 
}); 

我認爲問題在於事件綁定到元素後加載表。 $(document).on('click','element',function);確保事件總是被綁定,甚至在加載html之前。

或者使用.children返回多個元素,所以不能提供一個id。

+0

我不明白e實際上是什麼?此外,該表首先被加載,然後我輸入上面寫的js。那是錯誤的地方嗎? –

1
Try this: 



$(document).on('click', '.rowclass', function() { 
     $(this).find('a').trigger('click'); 
}); 

LE:

jQuery(document).ready(function(){ 
    jQuery(document).on('click', '.rowclass', function(e) { 
     var link = $(this).find("a"); 
     if (e.target === link[0]) return false; 
     link.trigger('click'); 
     return false; 
    }); 
}) 

如果(e.target ===鏈接[0]) - 在這裏,我們檢查,如果點擊的元素是一個標籤本身。如果是這樣,我們會讓默認行爲發生。 如果沒有,我們觸發標籤點擊

+0

雖然此代碼片段可能是解決方案,但[包括解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-基於代碼的答案)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – Adam

+0

@Vladut這不起作用,因爲它會出現以下錯誤:「未捕獲的RangeError:超出最大調用堆棧大小」。如果我在循環中有'console.log'任何東西,它就會進入一個無限循環 –

+0

是的,對不起。它並沒有跨越我的想法觸發一個孩子的點擊事件,它也將被視爲點擊父母。這就是錯誤所說的。 此代碼應該可以正常工作: 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

相關問題