简体   繁体   English

重写 <tr> onclick,在单个单元格中切换模态

[英]Overriding <tr> onclick, toggle modal in single cell

I have a table where each row is clickable using 我有一个表格,其中每一行都可以使用

 echo "<tr class=\"trlink\" onclick=\"location.href='".$row['task_id']."'\">\n";

No issues there. 那里没有问题。

I then have three links inside one cell in this row. 然后,在这一行的一个单元格内有三个链接。 The two that point to different pages work fine. 指向不同页面的两个可以正常工作。 There is one link however that toggles a modal dialog, it will not display with the onclick included in the tag. 但是,有一个链接可以切换模式对话框,它不会显示为包含标签中的onclick的对话框。 It works fine without. 没有它可以正常工作。 I use the following as an 我将以下内容用作

<a role=\"button\" href=\"#" . $row['task_id'] . "taskModal\" data-toggle=\"modal\">

Whenever the link is clicked, it goes straight to the href assigned to the . 只要点击链接,链接就会直接指向分配给的href。 I assume the issue lies in event bubbling. 我认为问题出在事件冒泡。 I did some googling and found a small Javascript script to cancel the bubbling on click of the modal trigger: 我进行了一些谷歌搜索,发现一个小的Javascript脚本可以消除点击模式触发器时的冒泡:

function cancelIt(evt) {
var e = (typeof evt != 'undefined') ? evt : event;
e.cancelBubble = true;
}

And

<a href=\"...\" onclick=\"cancelIt(event);\" >

on the modal trigger 在模式触发

This solves the problem of loading the initial link when clicked, however the modal does not display either. 这解决了单击时加载初始链接的问题,但是模态也不会显示。

Thanks for any assistance. 感谢您的协助。

I don't recommend using the onclick event if you're instrumenting your pages with jQuery. 如果您使用jQuery检测页面,则不建议使用onclick事件。 Instead, use a click handler assigned programatically. 相反,请使用以编程方式分配的click处理程序。 This ensures you're not accidentally stomping on something. 这样可以确保您不会意外踩踏某些东西。

Without more details (perhaps a jsfiddle), it's difficult to say for sure what your particular problem is, but one thing you can try is to add some kind of selector to the appropriate anchor tag, then use a paradigm similar to: 没有更多细节(也许是jsfiddle),很难确定您的特定问题是什么,但是您可以尝试的一件事是在适当的锚标记中添加某种选择器,然后使用类似于以下的范例:

<script>
  $(function() {
    $('#anchorTagICareAbout').click(function(e) {
      e.preventDefault();
      displayModalDialog();
    });

  });
</script>

You could simply use the "onmousedown" event instead. 您可以直接使用“ onmousedown”事件。

<a role=\"button\" href=\"#" . $row['task_id'] . "taskModal\" data-toggle=\"modal\" 
onmousedown=\"event.cancelBubble=true;event.returnValue=false;document.location.hash=this.getAttribute('href').replace('#','');return false;\">

Here's a little demo jsfiddle: http://jsfiddle.net/r4dnp/6/ 这是jsfiddle的一些演示: http : //jsfiddle.net/r4dnp/6/

Using proper script assigned event handlers is always preferred, but the above will get the job done. 始终首选使用分配有适当脚本的事件处理程序,但是上述操作可以完成工作。 The key part of the code: 代码的关键部分:

document.location.hash=this.getAttribute('href').replace('#','')

Sets the document.location.hash equal to whatever the value of the links HREF is. 将document.location.hash设置为等于链接HREF的值。

I have a working solution . 我有一个可行的解决方案 It might be a hack way to do it, but it works. 这可能是一种破解方法,但它可以工作。 Note I also moved the modal <div> outside of the <tr> because when I closed the modal it would follow the original onclick event for the <tr> . 注意,我还将模式<div>移到了<tr> <div>之外,因为当我关闭模式时,它将遵循<tr>的原始onclick事件。

<script type="text/javascript">
var trlink = true;
</script>

Above sets a variable to check when clicking tr onclick . 上面设置了一个变量,用于在单击tr onclick时进行检查。

<tr onclick=\"if(trlink) {location.href='google.com'} else { trlink = true; }>

This checks if the trlink is true before opening link, if not, sets it true. 这会在打开链接之前检查trlink是否为true,否则将其设置为true。

<a onclick="trlink = false" role="button" href="#1printerModal" data-toggle="modal">None</a>

Finally this sets the trlink variable to false before performing any actions in this tag. 最后,在执行此标签中的任何操作之前,这会将trlink变量设置为false。

I couldn't get the event handling stuff correct, this seems to do the trick though. 我无法正确处理事件,但是这似乎可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM