2017-04-12 80 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

相关问题