jQuery 单击表行切换部分

发布于 2024-09-10 12:34:16 字数 316 浏览 3 评论 0原文

我有以下标记(我无法控制,必须保持原样,但是可以操纵 JavaScript。)

http ://jsbin.com/uyifu3/edit

我想要完成的是,当单击表行时,会自动单击“在此处切换我”链接。

我不断发现自己在当前设置下陷入了递归循环。

因此,单击表行中的任意位置(减去实际单击链接本身)应该单击将切换红色框的链接。单击独立于表行的链接应正常切换红色框,而不调用触发的“selectedRow”单击事件。

I have the following markup (which I have no control and must remain as is, however the JavaScript can be manipulated.)

http://jsbin.com/uyifu3/edit

What I'm trying to accomplish is that when the table row is clicked the the "Toggle Me Here" link is automatically clicked.

I keep finding my self getting into a recursive loop with the current setup.

So clicking anywhere in the table row (minus actually clicking on the link itself) should click the link which there for will toggle the red box. click on the link independent of the table row should toggle the red box as normal without calling the 'selectedRow' click event being fired.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

芸娘子的小脾气 2024-09-17 12:34:16

免责声明,以下答案不是最佳解决方案。如果可能的话,请使用 @karim 的解决方案,使用不显眼的处理程序。

如果他的解决方案不是一个选项,例如此链接是由某些东西自动生成的,那么您可以执行以下操作:

$(function() {
  $('.selectedRow').click(function(e) {
    if(e.target.nodeName != 'A')
      $(this).find('td:last a').click();
  });
});

您可以在此处进行测试,它只是检查点击是否首先来自锚点。如果是这种情况,它已经完成了需要做的事情,冒泡到该行的 click 事件不应该采取任何操作。

Disclaimer, the below answer isn't an optimal solution. Go with @karim's solution using unobtrusive handlers if at all possible.

If his solution is not an option, e.g. this link is auto-generated by something, then you can do this:

$(function() {
  $('.selectedRow').click(function(e) {
    if(e.target.nodeName != 'A')
      $(this).find('td:last a').click();
  });
});

You can test it here, it just checks that the click didn't originate from the anchor in the first place. If that's the case, it's done what it need to do, the click event bubbling up to the row should take no action.

差↓一点笑了 2024-09-17 12:34:16

(我认为)发生的情况是,当您单击锚点时,事件会冒泡并触发绑定到该行的事件。我建议按如下方式绑定到锚点:

  $('.toggleMe').click(function(e) {
      e.stopPropagation(); // prevent event from bubbling up the DOM tree
      $("#box").toggle();        
  });

另外,从锚点中删除内联函数调用,并为其提供类 .toggleMe 或此类(在上面的单击处理程序中使用):

<a class="toggleMe" href="#">Toggle Me Here</a>

在这里尝试: http://jsbin.com/uyifu3/7/edit (点击锚点将< em>没有触发.selectedRow点击事件,我向其中添加了警报)。

What is happening (I think) is that when you click on the anchor, the event bubbles up and triggers the one bound to the row. I would suggest binding to the anchor as follows:

  $('.toggleMe').click(function(e) {
      e.stopPropagation(); // prevent event from bubbling up the DOM tree
      $("#box").toggle();        
  });

Also, remove the inline function call from the anchor, and give it the class .toggleMe or such (used in the above click handler):

<a class="toggleMe" href="#">Toggle Me Here</a>

Try it here: http://jsbin.com/uyifu3/7/edit (clicking on the anchor will not trigger the .selectedRow click event, to which I added an alert).

方圜几里 2024-09-17 12:34:16

尝试退回到公共父级(本例中的 row/tr),然后向下钻取到目标(带有 onclick 处理程序的目标)。

$(e.target).parents('tr').first().find('td:last a').click();

Try backing out to a common parent (the row/tr in this example) and then drill back down to the target (a with an onclick handler).

$(e.target).parents('tr').first().find('td:last a').click();
最偏执的依靠 2024-09-17 12:34:16
$('tr').click(function(){
    $('#box').toggle();
});

只需删除 toggleMe() 的任何功能,这样单击链接本质上与单击行没有什么不同。它只是为了展示:)

$('tr').click(function(){
    $('#box').toggle();
});

and just remove any functionality for toggleMe() so that essentially clicking the link is no different than clicking the row. it's just there for show :)

寂寞笑我太脆弱 2024-09-17 12:34:16

我假设有不止一行(因为您正在为 TR 使用一个类)。此代码对您有用:

$(document).ready(function(){
  // Remove the anchors
  $(".selectedRow td:last a").each(function(){
    $(this).replaceWith($(this).html());
  });

  // Make entire row clickable
  $(".selectedRow").click(function(e){
    $('#box').toggle();
  });
});

本质上,它删除了 toggleMe() 锚点并使整行可单击。

这是实际代码

I assume that there's more than one row (since you're using a class for the TR). This code would work for you:

$(document).ready(function(){
  // Remove the anchors
  $(".selectedRow td:last a").each(function(){
    $(this).replaceWith($(this).html());
  });

  // Make entire row clickable
  $(".selectedRow").click(function(e){
    $('#box').toggle();
  });
});

Essentially it removes the toggleMe() anchors and makes the entire row clickable.

Here's the code in action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文