jQuery 单击表行切换部分
我有以下标记(我无法控制,必须保持原样,但是可以操纵 JavaScript。)
我想要完成的是,当单击表行时,会自动单击“在此处切换我”链接。
我不断发现自己在当前设置下陷入了递归循环。
因此,单击表行中的任意位置(减去实际单击链接本身)应该单击将切换红色框的链接。单击独立于表行的链接应正常切换红色框,而不调用触发的“selectedRow”单击事件。
I have the following markup (which I have no control and must remain as is, however the JavaScript can be manipulated.)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
免责声明,以下答案不是最佳解决方案。如果可能的话,请使用 @karim 的解决方案,使用不显眼的处理程序。
如果他的解决方案不是一个选项,例如此链接是由某些东西自动生成的,那么您可以执行以下操作:
您可以在此处进行测试,它只是检查点击是否首先来自锚点。如果是这种情况,它已经完成了需要做的事情,冒泡到该行的
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:
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.(我认为)发生的情况是,当您单击锚点时,事件会冒泡并触发绑定到该行的事件。我建议按如下方式绑定到锚点:
另外,从锚点中删除内联函数调用,并为其提供类
.toggleMe
或此类(在上面的单击处理程序中使用):在这里尝试: 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:
Also, remove the inline function call from the anchor, and give it the class
.toggleMe
or such (used in the above click handler):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).尝试退回到公共父级(本例中的 row/tr),然后向下钻取到目标(带有 onclick 处理程序的目标)。
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).
只需删除
toggleMe()
的任何功能,这样单击链接本质上与单击行没有什么不同。它只是为了展示:)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 :)我假设有不止一行(因为您正在为
TR
使用一个类)。此代码对您有用:本质上,它删除了
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:Essentially it removes the
toggleMe()
anchors and makes the entire row clickable.Here's the code in action.