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
相关问题